从日志看 Nginx 性能,核心思路是先明确要看哪些日志字段,再结合指标和工具做分析。下面按“日志类型 → 关键指标 → 实操方法”给你一个完整思路。
最常用,记录每一次请求的情况。
默认路径:
/var/log/nginx/access.log
看异常、超时、连接问题。
/var/log/nginx/error.log
实时看连接和请求数(不是日志,但非常关键)。
默认日志没有响应时间,需要自定义 log_format:
log_format main '$remote_addr - $request_time - $upstream_response_time';
$request_time:Nginx 处理整个请求的时间$upstream_response_time:后端(PHP/Java)响应时间怎么看:
统计单位时间请求数:
wc -l access.log
或按分钟统计:
awk '{print $4}' access.log | cut -c 2-18 | uniq -c
看异常比例:
awk '{print $9}' access.log | sort | uniq -c | sort -rn
重点关注:
awk '{print $request_time, $7}' access.log | sort -rn | head
或只看成对后端慢的:
awk '$upstream_response_time > 1 {print}' access.log
awk '{print $10, $7}' access.log | sort -rn | head
$10 是响应字节数。
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head
用于发现:
重点看:
timeouttoo many open filesworker_connectionsupstream timed out示例:
upstream timed out (110: Connection timed out)
说明:后端响应慢或连接池不够。
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
Active connections: 200
server accepts handled requests
10000 10000 50000
Reading: 10 Writing: 20 Waiting: 170
指标含义:
goaccess access.log -o report.html
适合生产环境长期分析。
Nginx 性能看日志 = 响应时间 + 状态码 + QPS + 错误日志 + 实时连接数
如果你愿意,我可以: