HTTP 请求变量
请求信息变量
# 基础请求信息
$request_method # 请求方法:GET, POST, PUT, DELETE 等
$request_uri # 完整请求URI(包含查询参数):/path?arg=value
$uri # 当前请求的URI(不含参数):/path
$document_uri # 同 $uri
$query_string # 查询字符串:arg=value&foo=bar
$args # 同 $query_string
$is_args # 如果有查询参数则为 "?",否则为空
$arg_PARAMETER # 获取特定参数值:$arg_id 获取 ?id=123 中的 123
请求头变量
# 客户端请求头
$http_HEADER # 任意请求头:$http_user_agent, $http_referer
$http_user_agent # 用户代理字符串
$http_referer # 来源页面URL
$http_cookie # Cookie 字符串
$http_host # 请求的 Host 头
$http_accept # Accept 头
$http_accept_language # Accept-Language 头
$http_accept_encoding # Accept-Encoding 头
$http_connection # Connection 头
$http_upgrade # Upgrade 头(WebSocket)
$http_x_forwarded_for # X-Forwarded-For 头
$http_x_real_ip # X-Real-IP 头
请求体变量
$content_length # Content-Length 请求头
$content_type # Content-Type 请求头
$request_body # 请求体内容(需要启用)
$request_body_file # 请求体临时文件路径
连接信息变量
连接属性
$connection # 连接序列号
$connection_requests # 当前连接上的请求数
$request # 完整的请求行:GET /index.html HTTP/1.1
$request_id # 请求的唯一标识(16进制)
$request_length # 请求长度(包括请求行、头部和请求体)
$request_time # 请求处理时间(秒,毫秒精度)
$remote_addr # 客户端IP地址
$remote_port # 客户端端口
$remote_user # 基本认证用户名
$server_addr # 服务器IP地址
$server_port # 服务器端口
$server_protocol # 请求协议:HTTP/1.0, HTTP/1.1
连接状态
$status # 响应状态码:200, 404, 500 等
$body_bytes_sent # 发送给客户端的字节数(不包括响应头)
$bytes_sent # 发送给客户端的字节数(包括响应头)
$pipe # 如果请求是管道化的则为 "p",否则为 "."
SSL/TLS 变量
SSL 连接信息
# SSL/TLS 相关
$ssl_protocol # TLS 协议版本:TLSv1.2, TLSv1.3
$ssl_cipher # 使用的加密套件
$ssl_session_id # SSL 会话ID
$ssl_session_reused # SSL 会话重用标志:"r" 或 "."
$ssl_early_data # TLS 1.3 0-RTT 数据标志
# 客户端证书
$ssl_client_serial # 客户端证书序列号
$ssl_client_s_dn # 客户端证书主题
$ssl_client_i_dn # 客户端证书颁发者
$ssl_client_verify # 客户端证书验证结果:SUCCESS, FAILED, NONE
$ssl_client_fingerprint # 客户端证书指纹
$ssl_client_raw_cert # 原始客户端证书(PEM格式)
时间变量
时间和日期
$time_iso8601 # ISO 8601 格式时间:2024-01-15T14:30:45+08:00
$time_local # 本地时间格式:15/Jan/2024:14:30:45 +0800
$msec # 当前时间(秒.毫秒):1705293045.123
$request_completion # 请求完成状态:OK, ""
$date_local # 本地日期
$date_gmt # GMT 日期
Nginx 核心变量
Nginx 内部信息
# 进程和连接
$pid # 工作进程PID
$hostname # 主机名
$nginx_version # Nginx 版本
$limit_rate # 连接速率限制
$connection_time # 连接持续时间
$upstream_response_time # 上游响应时间
请求处理阶段
$request_filename # 当前请求的文件路径
$realpath_root # 当前请求的根目录绝对路径
$document_root # 当前请求的根目录
$fastcgi_script_name # FastCGI 脚本名
$scheme # 请求协议:http 或 https
$https # 如果是 HTTPS 则为 "on",否则为空
Upstream 变量
负载均衡后端信息
$upstream_addr # 上游服务器地址:192.168.1.1:8080
$upstream_status # 上游服务器响应状态码
$upstream_response_time # 上游响应时间(秒,毫秒精度)
$upstream_connect_time # 连接到上游服务器的时间
$upstream_header_TIME # 上游响应头:$upstream_header_content_type
$upstream_cookie_NAME # 上游响应Cookie:$upstream_cookie_sessionid
$upstream_http_NAME # 上游响应头:$upstream_http_server
$upstream_cache_status # 缓存状态:MISS, HIT, EXPIRED, etc.
Location 和 Rewrite 变量
Rewrite 模块变量
# 重写相关
$ancient_browser # 古董浏览器标志
$modern_browser # 现代浏览器标志
$msie # 如果是 IE 浏览器则为 1
# 地理位置(需要 ngx_http_geoip_module)
$geoip_country_code # 国家代码:CN, US, JP
$geoip_country_name # 国家名称
$geoip_city # 城市名
$geoip_region # 地区/州
限流模块变量
# 限流相关(需要 ngx_http_limit_req_module)
$limit_req_status # 限流状态:PASSED, DELAYED, REJECTED
# 连接限制(需要 ngx_http_limit_conn_module)
$limit_conn_status # 连接限制状态:PASSED, REJECTED
日志格式常用变量
组合日志格式
# 自定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format json escape=json '{'
'"time_local":"$time_local",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"status":"$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_time":"$request_time",'
'"http_referer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"upstream_addr":"$upstream_addr",'
'"upstream_response_time":"$upstream_response_time"'
'}';