Nginx 配置文件解析
核心配置文件结构
nginx
# 全局块(主配置)
main {
# 事件块
events {
...
}
# HTTP块
http {
# 服务器块
server {
# 位置块
location {
...
}
}
}
# 邮件代理块(可选)
mail {
...
}
}关键配置模块详解
全局块(Main Context)
控制 Nginx 的全局行为,作用于所有模块。
常用指令:
nginxuser www; # 运行用户 worker_processes auto; # 工作进程数(通常设为CPU核心数) error_log /var/log/nginx/error.log warn; # 错误日志路径及级别 pid /var/run/nginx.pid; # PID文件路径 worker_rlimit_nofile 65535; # 单个进程最大文件打开数
事件块(Events Context)
配置连接处理机制。
关键参数:
nginxevents { worker_connections 2048; # 每个Worker的最大连接数 multi_accept on; # 是否一次性接受所有新连接 use epoll; # 事件驱动模型(Linux推荐epoll) }
HTTP块(HTTP Context)
包含所有HTTP相关配置,可嵌套多个
server块。核心功能:
nginxhttp { include mime.types; # 包含MIME类型定义文件 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; # 启用高效文件传输模式 tcp_nopush on; # 优化数据包发送 keepalive_timeout 65; # 长连接超时时间(秒) gzip on; # 启用Gzip压缩 gzip_types text/plain text/css application/json; # 压缩类型 # 服务器块(虚拟主机) server { ... } }
Server块(Server Context)
定义虚拟主机,每个
server对应一个域名或IP。典型配置:
nginxserver { listen 80; # 监听端口 server_name example.com www.example.com; # 域名 # 根目录配置 root /var/www/example.com; index index.html; # 错误页面 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; }
Location块(Location Context)
匹配URI路径,处理特定请求。
匹配规则与示例:
nginxserver { listen 80; # 监听80端口 listen 443 ssl; # 监听443端口 server_name example.com www.example.com; # 域名 ssl_certificate /etc/nginx/ssl/example.com.crt; # ssl证书认证文件 ssl_certificate_key /etc/nginx/ssl/example.com.key; # ssl证书key # 强制HTTPS if ($scheme != "https") { return 301 https://$host$request_uri; } root /var/www/html; # 网站根目录 index index.html index.php; # 默认首页 # 错误页面 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 基础安全配置 server_tokens off; # 隐藏Nginx版本号 add_header X-Frame-Options "SAMEORIGIN"; # 防点击劫持 # 位置块(URL路由) location / { try_files $uri $uri/ /index.php?$query_string; } # 转发PHP location ~ \.php$ { # 监控sock文件 fastcgi_pass unix:/var/run/php-fpm.sock; # fastcgi_pass 127.0.0.1:9000 include fastcgi_params; } # 静态文件缓存 location ~* \.(jpg|css|js)$ { expires 30d; access_log off; } # 反向代理 location /api/ { proxy_pass http://127.0.0.1:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
Nginx 变量
HTTP 请求相关变量
| 变量名 | 说明 |
|---|---|
$request_method | HTTP 请求方法(GET、POST 等) |
$request_uri | 完整的原始请求 URI(包括查询参数,如 /path?a=1) |
$uri | 解码后的请求 URI(不含查询参数,如 /path) |
$args | 查询字符串(a=1&b=2) |
$query_string | 同 $args |
$is_args | 如果有查询参数,值为 ?,否则为空 |
$scheme | 协议(http 或 https) |
$http_<header> | 任意请求头,如 $http_user_agent(浏览器 UA)、$http_referer(来源页) |
$host | 请求的 Host(优先取 Host 头,若无则取 server_name) |
$server_name | 当前虚拟主机的 server_name |
$remote_addr | 客户端 IP 地址 |
$remote_port | 客户端端口 |
$server_addr | 服务器 IP 地址 |
$server_port | 服务器端口 |
$https | 如果是 HTTPS 请求,值为 on,否则为空 |
HTTP 响应相关变量
| 变量名 | 说明 |
|---|---|
$status | HTTP 响应状态码(如 200、404) |
$body_bytes_sent | 发送给客户端的 body 字节数 |
$bytes_sent | 发送给客户端的总字节数(含 header) |
$sent_http_<header> | 响应头,如 $sent_http_content_type |
连接与请求处理变量
| 变量名 | 说明 |
|---|---|
$connection | 连接序列号 |
$connection_requests | 当前连接上的请求数 |
$request_time | 请求处理时间(秒,含响应传输时间) |
$upstream_response_time | 上游服务器响应时间(用于反向代理) |
$request_length | 请求的字节数(含请求行、header、body) |
$time_local | 本地时间(日志格式默认时间) |
$msec | 当前时间戳(毫秒,如 1718784000.123) |
反向代理(Upstream)相关变量
| 变量名 | 说明 |
|---|---|
$upstream_addr | 上游服务器地址(如 127.0.0.1:8080) |
$upstream_status | 上游服务器返回的状态码 |
$upstream_http_<header> | 上游服务器的响应头,如 $upstream_http_server |
SSL/TLS 相关变量
| 变量名 | 说明 |
|---|---|
$ssl_protocol | SSL/TLS 协议版本(如 TLSv1.2) |
$ssl_cipher | 加密套件(如 AES256-SHA) |
$ssl_client_cert | 客户端证书(PEM 格式) |
$ssl_session_id | SSL 会话 ID |
其他实用变量
| 变量名 | 说明 |
|---|---|
$request_id | 唯一请求标识(可用于日志追踪) |
$limit_rate | 客户端限速(可动态修改,单位:字节/秒) |
$nginx_version | Nginx 版本号 |
$pid | 当前 Worker 进程的 PID |
