文章目录
Nginx 日志格式配置全面指南
一、核心日志配置指令
1. log_format
指令语法
log_format format_name 'string_format';
2. access_log
指令语法
access_log path [format [buffer=size] [gzip[=level]] [flush=time]];
二、完整可配置字段列表
1. 基础请求信息
变量 | 说明 | 示例值 |
---|---|---|
$remote_addr | 客户端IP地址 | 192.168.1.100 |
$remote_user | 客户端用户名(基本认证) | admin |
$time_local | 本地时间(通用日志格式) | 01/Jan/2023:12:34:56 +0800 |
$time_iso8601 | ISO8601格式时间 | 2023-01-01T12:34:56+08:00 |
$request | 完整请求行 | GET /index.html HTTP/1.1 |
$request_method | 请求方法 | GET , POST |
$request_uri | 完整请求URI(含参数) | /search?q=nginx |
$uri | 请求URI(不含参数) | /search |
$args | 查询字符串 | q=nginx&page=1 |
$query_string | 同 $args | q=nginx&page=1 |
$server_protocol | 请求协议 | HTTP/1.1 |
$server_name | 服务器名称 | example.com |
$server_addr | 服务器IP地址 | 192.168.1.1 |
$server_port | 服务器端口 | 80 , 443 |
$status | 响应状态码 | 200 , 404 , 500 |
$body_bytes_sent | 发送给客户端的字节数 | 1024 |
$bytes_sent | 发送的总字节数(含头) | 1256 |
$request_length | 请求长度(含头) | 356 |
2. 连接与性能指标
变量 | 说明 | 示例值 |
---|---|---|
$connection | 连接序列号 | 123456 |
$connection_requests | 当前连接上的请求数 | 5 |
$request_time | 请求处理时间(秒) | 0.056 |
$upstream_response_time | 后端响应时间 | 0.045 |
$upstream_connect_time | 连接后端时间 | 0.012 |
$upstream_header_time | 首字节时间 | 0.023 |
$pipe | 是否使用管道 | p (pipelined) |
$msec | 当前时间(毫秒精度) | 1672547696.123 |
3. HTTP 头信息
变量 | 说明 | 示例值 |
---|---|---|
$http_user_agent | 客户端浏览器信息 | Mozilla/5.0 |
$http_referer | 来源页面URL | https://google.com |
$http_host | 请求主机头 | www.example.com |
$http_x_forwarded_for | 代理链IP地址 | 192.168.1.100, 10.0.0.1 |
$http_cookie | Cookie信息 | sessionid=abc123 |
$http_accept_language | 客户端语言 | en-US,en;q=0.9 |
$http_accept_encoding | 客户端编码 | gzip, deflate |
$http_accept | 客户端接受类型 | text/html |
$http_authorization | 认证信息 | Basic YWRtaW46cGFzc3dvcmQ= |
4. 反向代理相关
变量 | 说明 | 示例值 |
---|---|---|
$upstream_addr | 后端服务器地址 | 10.0.0.2:8080 |
$upstream_status | 后端响应状态码 | 200 |
$upstream_cache_status | 缓存状态 | HIT , MISS , EXPIRED |
$upstream_http_* | 后端响应头 | $upstream_http_server |
$proxy_host | 代理主机名 | backend-server |
$proxy_port | 代理端口 | 8080 |
5. SSL/TLS 信息
变量 | 说明 | 示例值 |
---|---|---|
$ssl_protocol | SSL协议版本 | TLSv1.2 |
$ssl_cipher | 加密套件 | ECDHE-RSA-AES128-GCM-SHA256 |
$ssl_session_id | SSL会话ID | abcdef1234567890 |
$ssl_session_reused | 会话重用 | r (reused) |
$ssl_client_cert | 客户端证书 | -----BEGIN CERTIFICATE-----... |
$ssl_client_verify | 客户端证书验证 | SUCCESS , FAILED |
$ssl_server_name | SNI服务器名 | secure.example.com |
三、日志格式配置示例
1. 基础日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
2. 扩展日志格式(含性能指标)
log_format extended '$remote_addr - $remote_user [$time_iso8601] '
'"$request_method $request_uri $server_protocol" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct=$upstream_connect_time '
'uht=$upstream_header_time urt=$upstream_response_time '
'cs=$upstream_cache_status';
3. JSON 格式日志
log_format json escape=json '{'
'"time": "$time_iso8601", '
'"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_response_time": "$upstream_response_time", '
'"upstream_cache_status": "$upstream_cache_status"'
'}';
四、高级配置技巧
1. 条件日志记录
map $status $loggable {
~^[23] 1; # 记录2xx/3xx
default 0; # 不记录其他
}
access_log /var/log/nginx/access.log combined if=$loggable;
2. 多日志文件分离
# 错误日志单独记录
map $status $error_log {
~^[45] 1; # 4xx/5xx错误
default 0;
}
access_log /var/log/nginx/error.log combined if=$error_log;
access_log /var/log/nginx/access.log combined;
3. 性能优化配置
access_log /var/log/nginx/access.log combined
buffer=32k # 32KB缓冲区
flush=5m; # 5分钟刷新
五、完整配置示例
http {
# 定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
log_format extended '$remote_addr - $remote_user [$time_iso8601] '
'"$request_method $request_uri $server_protocol" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct=$upstream_connect_time '
'uht=$upstream_header_time urt=$upstream_response_time '
'cs=$upstream_cache_status';
log_format json escape=json '{'
'"time": "$time_iso8601", '
'"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_response_time": "$upstream_response_time", '
'"upstream_cache_status": "$upstream_cache_status"'
'}';
# 条件日志记录
map $status $loggable {
default 1;
"499" 0; # 不记录客户端断开连接
}
map $status $error_status {
~^[45] 1; # 4xx/5xx错误
default 0;
}
server {
listen 80;
server_name example.com;
# 主访问日志
access_log /var/log/nginx/access.log extended buffer=32k flush=5m if=$loggable;
# 错误日志单独记录
access_log /var/log/nginx/error.log extended if=$error_status;
# JSON格式日志
access_log /var/log/nginx/access.json json;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
# 记录后端信息
log_format backend '$remote_addr - $upstream_addr [$time_iso8601] '
'"$request" $upstream_status $body_bytes_sent '
'rt=$request_time urt=$upstream_response_time';
access_log /var/log/nginx/backend.log backend;
}
# 静态文件不记录
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
access_log off;
expires 30d;
}
}
}
六、日志分析建议
1. 推荐字段组合
场景 | 推荐字段 |
---|---|
基础监控 | $remote_addr , $time_iso8601 , $request , $status , $body_bytes_sent |
性能分析 | $request_time , $upstream_response_time , $upstream_connect_time , $upstream_header_time |
安全审计 | $http_user_agent , $http_referer , $http_x_forwarded_for , $request_method |
缓存优化 | $upstream_cache_status , $uri , $args |
SEO分析 | $http_referer , $http_user_agent , $uri , $args |
2. 日志轮转配置
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
/usr/bin/systemctl reload nginx > /dev/null
endscript
}
七、常见问题解决
1. 日志字段为空
# 确保变量在正确上下文中可用
# SSL变量只在HTTPS服务器中有效
# 上游变量只在代理请求中有效
2. 特殊字符处理
# JSON格式使用escape=json
log_format json escape=json '{...}';
# CSV格式处理逗号
log_format csv '$remote_addr,$time_local,"$request",$status';
3. 性能影响优化
# 减少日志字段数量
# 增加缓冲区大小
# 关闭静态资源日志
# 使用syslog代替文件
八、最佳实践总结
-
格式选择原则:
- 调试分析:使用扩展格式
- 日志分析系统:使用JSON格式
- 兼容性:使用基础格式
-
关键字段包含:
# 必须包含的字段 $time_iso8601, $remote_addr, $request_method, $request_uri, $status, $request_time
-
安全注意事项:
- 避免记录敏感信息(密码、token)
- 限制日志文件权限
- 定期清理历史日志
-
性能优化:
access_log /path/to/log buffer=64k flush=10m; location ~* \.(static)$ { access_log off; }
-
监控字段:
- 错误率:
$status ~ "^[45]"
- 慢请求:
$request_time > 1
- 缓存命中率:
$upstream_cache_status
- 错误率:
通过合理配置Nginx日志格式,您可以获得丰富的运维洞察力,有效监控服务性能和安全性。