解析Nginx配置文件conf中的常用块

解析Nginx配置文件conf中的常用块

Nginx是一个高性能的HTTP和反向代理服务器,它的配置文件主要由多个块组成。本文将介绍Nginx配置文件中的常用块及其功能。

  1. events块
    events块用于设置Nginx的工作模式和连接数相关的参数。
bash 复制代码
events {
    worker_connections  1024; # 单个工作进程的最大连接数
}
  1. http块
    http块用于配置Nginx处理HTTP请求的相关参数。
bash 复制代码
http {
    include       mime.types; # 包含MIME类型文件
    default_type  application/octet-stream; # 默认MIME类型
    sendfile        on; # 开启高效文件传输模式
    keepalive_timeout  65; # 长连接超时时间(秒)
    gzip  on; # 开启GZIP压缩
}
  1. server块
    server块用于配置一个虚拟主机。可以有多个server块,但只能有一个server块包含listen指令。
bash 复制代码
server {
    listen       80; # 监听端口
    server_name  example.com; # 域名
    root         /var/www/example.com; # 网站根目录
    index        index.html index.htm; # 默认首页文件名
}
  1. location块
    location块用于匹配URL路径,并配置相应的处理方式。可以有多个location块。

4.1 location /块

bash 复制代码
location / {
    root   /usr/share/nginx/html; # 网站根目录
    index  index.html index.htm; # 默认首页文件名
}

4.2 location ~ .php$块

bash 复制代码
location ~ \.php$ {
    root           /usr/share/nginx/html; # PHP脚本根目录
    fastcgi_pass   127.0.0.1:9000; # PHP-FPM地址和端口
    fastcgi_index  index.php; # PHP-FPM默认文件名
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; # PHP-FPM参数
    include        fastcgi_params; # 包含PHP-FPM参数文件
}
  1. upstream块
    upstream块用于配置后端服务器组,实现负载均衡。可以有多个upstream块。
bash 复制代码
upstream backend {
    server backend1.example.com weight=3; # 权重为3的后端服务器1
    server backend2.example.com; # 后端服务器2,权重默认为1
}
  1. proxy_pass指令
    proxy_pass指令用于将请求转发到后端服务器。通常用在location或if块中。
bash 复制代码
location /api {
    proxy_pass http://backend; # 将请求转发到名为backend的upstream中定义的服务器组
}
  1. try_files指令
    try_files指令用于尝试按顺序查找文件。如果找到,则直接返回文件内容;否则,继续查找其他指令。通常用在location或if块中。
bash 复制代码
location / {
    try_files $uri $uri/ =404; # 按顺序查找文件,找不到则返回404错误页面
}
  1. map指令和split_clients模块(仅适用于Nginx Plus)
    map指令和split_clients模块可以实现基于客户端IP地址的会话亲和性。这在需要将特定用户群分配到特定服务器的场景中非常有用。需要注意的是,这个功能仅适用于Nginx Plus版本。

首先,需要在编译Nginx时启用split_clients模块:

bash 复制代码
--with-http_split_clients_module # 在configure命令中添加该选项,然后重新编译Nginx

然后,在配置文件中使用map指令和split_clients模块:

bash 复制代码
http {
    ...
    map $remote_addr $backend { # 根据客户端IP地址映射到后端服务器组名的变量值,例如:192.168.1.1 -> "backend1",192.168.1.2 -> "backend2" ... } # 根据实际需求自定义映射关系 split_clients "$backend"; # 根据上一步定义的变量值进行会话亲和性分配 ... server { ... } ... } } } } } } } } } } } } } } } } } } } } } } } } } } } } ... server { ... } ... } } } } } } } ... } } ... } ... } ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... # 根据实际需求自定义后端服务器组和相关配置信息... server { listen 80; server_name example.com; location / { root /var/www/example.com; index index.html index.htm; try_files $uri $uri/ =404; } location /api { map $backend $backend_host { default "backend1"; ~^backend[1-9]$ "backend2"; ~^backend[1-9][0-9]$ "backend3"; ~^backend[1-9][0-9][0-9]$ "backend4"; ~^backend[1-9][0-9][0-9][0-9]$ "backend5"; ~^backend[1-9][0-9][0-9][0-9][0-9]$ "backend6"; ~^backend[1-9][0-9][0-9][0-9][0-9][0-9]$ "backend7"; ~^backend[1-9][0-9][0-9][0-9][0-9][0-9][0-9]$ "backend8"; ~^backend[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$ "backend9"; } set $backend "${backend}${remote_addr}"; set $backend_host "${backend_host}:8080"; proxy_pass http://$backend_host; }}}'d

结尾

以上就是本期的全部内容!一键三连拜谢!

相关推荐
威嵌神州20 小时前
嵌入式排故实录:高并发下串口数据“超时丢包”的根因剖析与架构优化
运维·服务器·嵌入式硬件·缓存
Decay__21 小时前
SQLServer 索引与 SQL 优化 —— 聚集索引、非聚集索引、覆盖索引、碎片维护与执行计划
运维·数据库·sql·mysql·架构
java_logo21 小时前
Docker 部署 DeepSeek Harness(moelin):轻松搭建带登录的本地 AI Agent 平台
运维·docker·容器·ai agent·deepseek·harness·轩辕镜像
AI_Gump1 天前
Harness篇|DeepSeek Harness 上手机:五条安卓移植路线与 App 内 Linux 拆解
android·linux·人工智能·智能手机·deepseek
打工仔折腾 AI1 天前
Changedetection.io 网页变动监控:Docker 部署、通知配置与公网暴露实践
运维·人工智能·后端·python·docker·容器·性能优化
无奈笑天下1 天前
银河麒麟上lsblk命令教程详解
linux·运维·服务器·数据库
fengkai45451 天前
Prometheus+K8s弹性伸缩综合项目
运维·kubernetes·k8s·prometheus·hpa·vpa
分布式存储与RustFS1 天前
镜像仓库的层别只堆本地盘:用 RustFS 给 Harbor 当 S3 后端
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
疯华整猫1 天前
否决 Flutter 提案那天,我发现自己也说不清 Framework 管什么
android
雪落漂泊1 天前
Liunux进程(七):程序控制篇(中)
linux·运维·windows