Nginx配置文件详细说明
封面图
•
2025-11-23
•
NGINX
252
0
0
0
Nginx 是一个高性能的 HTTP 和反向代理服务器,其配置文件是 `nginx.conf`,通常位于 `/etc/nginx/` 或 `/usr/local/nginx/conf/` 目录下。Nginx 的配置文件结构由多个块(blocks)组成,包括全局块、events 块、http 块、server 块和 location 块等。 --- ## 🔹 一、基本结构 Nginx 配置文件的基本结构如下: ```nginx # 全局块(全局配置) user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # events 块(事件处理配置) events { worker_connections 1024; } # http 块(HTTP 协议相关配置) http { include /etc/nginx/mime.types; default_type application/octet-stream; # 日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; # gzip 压缩 gzip on; # 包含其他配置文件 include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } ``` --- ## 🔹 二、各个块详解 ### 1. **全局块(Global Block)** - **`user`**:运行 Nginx 的用户和组(如 `nginx`)。 - **`worker_processes`**:Nginx 工作进程数,`auto` 表示自动根据 CPU 核心数分配。 - **`error_log`**:错误日志路径。 - **`pid`**:PID 文件路径。 ### 2. **events 块(Events Block)** - **`worker_connections`**:每个工作进程最大连接数。 - **`use`**:指定事件模型(如 `epoll`, `kqueue`, `select` 等),默认自动选择。 - **`multi_accept`**:是否接受多个连接(默认 `off`)。 ### 3. **http 块(HTTP Block)** 这是最核心的配置部分,包含所有与 HTTP 相关的设置。 #### 常见指令: | 指令 | 说明 | |------|------| | `include` | 引入其他配置文件(如 mime.types、conf.d 中的配置) | | `default_type` | 默认 MIME 类型 | | `log_format` | 定义日志格式 | | `access_log` | 访问日志路径和格式 | | `sendfile` | 是否使用 sendfile 传输文件(提升性能) | | `keepalive_timeout` | HTTP 连接保持时间 | | `gzip` | 启用 Gzip 压缩 | | `client_max_body_size` | 允许客户端请求体的最大大小 | | `proxy_set_header` | 设置代理头信息(用于反向代理) | --- ## 🔹 三、server 块(虚拟主机) 在 `http` 块中可以定义多个 `server` 块,代表不同的虚拟主机。 ```nginx server { listen 80; server_name example.com; # 反向代理 location / { proxy_pass http://backend; } # 静态文件服务 location /static/ { root /data/static; } # 错误页面 error_page 404 /404.html; } ``` ### 常见指令: | 指令 | 说明 | |------|------| | `listen` | 监听端口或 IP:PORT | | `server_name` | 虚拟主机名称(域名) | | `location` | 定义 URL 路径匹配规则 | | `root` | 静态文件根目录 | | `proxy_pass` | 反向代理目标地址 | | `rewrite` | URL 重写规则 | | `index` | 默认索引文件(如 `index.html`) | --- ## 🔹 四、location 块(URL 匹配) `location` 用于匹配请求的 URI,并根据规则进行处理。 ### 示例: ```nginx location / { root /data/www; index index.html; } location /images/ { root /data/images; } location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.sock; include fastcgi_params; } ``` ### 匹配规则: | 匹配方式 | 说明 | |----------|------| | `=` | 精确匹配 | | `~` | 正则匹配(区分大小写) | | `~*` | 正则匹配(不区分大小写) | | `^~` | 前缀匹配(优先于正则) | | `/` | 通用匹配(默认) | --- ## 🔹 五、常用配置示例 ### 1. **静态文件服务** ```nginx server { listen 80; server_name static.example.com; location / { root /var/www/static; index index.html; autoindex on; # 显示目录列表 } } ``` ### 2. **反向代理(Proxy)** ```nginx server { listen 80; server_name reverse.proxy.com; location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` ### 3. **负载均衡(Load Balancing)** ```nginx upstream backend { server 192.168.1.101 weight=3; server 192.168.1.102; server 192.168.1.103 backup; } server { listen 80; server_name lb.example.com; location / { proxy_pass http://backend; } } ``` --- ## 🔹 六、配置文件检查与重载 - **检查配置文件语法**: ```bash nginx -t ``` - **重新加载配置**(无需重启服务): ```bash nginx -s reload ``` --- ## 🔹 七、常用模块(可选) Nginx 支持很多第三方模块,如: - `ngx_http_ssl_module`:支持 HTTPS - `ngx_http_gzip_static_module`:支持静态压缩 - `ngx_http_fastcgi_module`:支持 FastCGI(如 PHP) - `ngx_http_proxy_module`:支持反向代理 - `ngx_http_rewrite_module`:支持 URL 重写 --- ## 🔹 八、配置文件结构总结 | 层级 | 名称 | 说明 | |------|------|------| | 1 | 全局块 | 全局配置 | | 2 | events 块 | 事件处理配置 | | 3 | http 块 | HTTP 协议配置 | | 4 | server 块 | 虚拟主机配置 | | 5 | location 块 | URL 路径匹配配置 | --- 如果你有具体的配置需求(如 HTTPS、负载均衡、反向代理、SSL 配置等),我可以为你提供更详细的示例和解释。欢迎继续提问!
上一篇:
nginx配置入门
下一篇:
CentOS 7 下安装 Nginx
标题录入,一次不能超过6条
留言
评论