定位 Nginx 日志异常,通常可以按下面这套思路从「看日志 → 分类型 → 查配置 → 复现 → 验证」逐步推进。
Nginx 主要有两类日志:
access.log(访问日志)
error.log(错误日志)
路径一般在:
/var/log/nginx/access.log
/var/log/nginx/error.log
grep " 500 " /var/log/nginx/access.log | wc -l
grep " 404 " /var/log/nginx/access.log | wc -l
awk '{print $9}' access.log | sort | uniq -c | sort -rn
tail -n 100 access.log | grep -E " 4[0-9]{2} | 5[0-9]{2} "
可能原因:
排查:
location /static/ {
alias /data/static/;
}
✅ 注意:alias 末尾是否带 /
502 Bad Gateway
504 Gateway Timeout
查看 error.log:
grep -E "upstream|timeout|connect" error.log
常见错误:
connect() failed (111: Connection refused)
upstream timed out (110: Connection timed out)
原因:
检查:
ls -l /path
ps aux | grep nginx
error_log /var/log/nginx/error.log debug;
nginx -t
nginx -s reload
⚠️ 生产环境调试完记得改回 error_log warn;
awk '{print $4}' access.log | cut -c 2-20 | uniq -c
可快速发现:
top
df -h
netstat -ant | grep :80 | wc -l
常见隐藏问题:
| 日志内容 | 含义 |
|---|---|
| upstream prematurely closed | 后端主动断连 |
| no live upstreams | upstream 全挂 |
| client intended to send too large body | 请求体过大 |
| recv() failed | 网络不稳定 |
如果你愿意,可以把具体异常日志内容或 nginx 配置贴出来,我可以直接帮你精准定位是哪一行配置或哪类问题。