🔹 请求相关变量
| 变量名 |
含义 |
示例值 |
| $request |
完整的原始请求行 |
GET /index.html HTTP/1.1 |
| $request_method |
请求方法 |
GET、POST |
| $request_uri |
原始请求 URI(包含参数) |
/index.html?user=1 |
| $uri |
规范化后的 URI(无参数) |
/index.html |
| $args |
查询字符串(不含 ?) |
user=1&token=abc |
| $is_args |
如果有参数则为 ?,否则为空 |
? |
| $query_string |
等同于 $args |
user=1&token=abc |
| $document_uri |
等同于 $uri |
/index.html |
| $document_root |
当前请求对应的 root 路径 |
/var/www/html |
| $request_filename |
documentrooturi 拼接后的真实路径 |
/var/www/html/index.html |
| $request_body |
客户端请求体(仅限特定模块) |
name=liren |
| $request_body_file |
临时存放请求体的文件路径 |
/tmp/nginx-client-body/0000001 |
| $request_time |
请求处理时间(秒) |
0.123 |
🔹 客户端信息
| 变量名 |
含义 |
示例值 |
| $remote_addr |
在 X-Real-IP 中追加客户端 IP |
192.168.1.10 |
| $remote_port |
客户端端口 |
53244 |
| $remote_user |
基本认证用户名 |
admin |
| $http_user_agent |
浏览器 User-Agent |
Mozilla/5.0... |
| $http_referer |
引荐页面 |
https://example.com/ |
| $http_cookie |
客户端发送的 Cookie |
sessionid=abc123 |
| $http_host |
客户端请求头中 Host 字段的值 |
example.com:8080 |
location / {
*# nginx的主机地址*
proxy_set_header Host $http_host*;*
*# 用户端真实的IP,即客户端IP*
proxy_set_header X-Real-IP $remote_addr*;*
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for*;*
proxy_pass http://localhost:8088*;*
}
🔹 服务端信息
| 变量名 |
含义 |
示例值 |
| $server_addr |
服务器 IP 地址 |
127.0.0.1 |
| $server_name |
匹配的 server_name |
localhost |
| $hostname |
主机名 |
MacBook-Air.local |
| $server_port |
服务器监听端口 |
80 |
| $scheme |
协议(http 或 https) |
http |
| $nginx_version |
当前 Nginx 版本 |
1.26.1 |
🔹 代理与转发
| 变量名 |
含义 |
示例值 |
| $proxy_host |
代理目标主机 |
backend |
| $proxy_port |
代理目标端口 |
8080 |
| $proxy_add_x_forwarded_for |
在 X-Forwarded-For 中追加客户端 IP |
192.168.1.10, 127.0.0.1 |
| $upstream_addr |
上游服务器地址 |
127.0.0.1:8080 |
| $upstream_status |
上游响应状态 |
200 |
| $upstream_response_time |
上游响应耗时 |
0.045 |
| $upstream_cache_status |
缓存命中状态 |
HIT、MISS、EXPIRED |
🔹 响应相关
| 变量名 |
含义 |
示例值 |
| $status |
HTTP 状态码 |
200 |
| $sent_http_content_type |
响应头 Content-Type |
text/html |
| $sent_http_content_length |
响应体长度 |
1024 |
| $body_bytes_sent |
实际发送给客户端的字节数 |
1536 |
| $connection |
当前连接序号 |
1024 |
| $connection_requests |
当前连接已处理的请求数 |
5 |
🔹 时间与日志
| 变量名 |
含义 |
示例值 |
| $time_local |
本地时间(日志格式) |
17/Oct/2025:22:13:11 +0800 |
| $time_iso8601 |
ISO 8601 时间 |
2025-10-17T22:13:11+08:00 |
| $msec |
当前时间戳(毫秒) |
1734473591 |
| $pipe |
连接是否为管道请求(p 或 .) |
. |
🔹 连接控制
| 变量名 |
含义 |
示例值 |
| $limit_rate |
限速(字节/秒) |
0(无限制) |
| $pid |
Nginx 进程 ID |
1298 |
| $tcpinfo_rtt |
TCP 往返时间(微秒) |
2543 |