Nginx 日志报错排查,一般可以按下面思路一步步来:
Nginx 主要有两类日志:
访问日志(access.log)
/var/log/nginx/access.log
错误日志(error.log)
/var/log/nginx/error.log
查看日志路径配置:
nginx -T | grep -E "access_log|error_log"
tail -n 100 /var/log/nginx/error.log
常见错误类型:
permission denied
chown -R nginx:nginx /path
chmod 755 /path
No such file or directory
Address already in use
netstat -tunlp | grep 80
unknown directive
nginx -t
如果有错误,会直接指出文件和行号。
tail -n 50 /var/log/nginx/access.log
关注:
例如:
GET /api/test 502
说明后端服务有问题
| 状态码 | 常见原因 |
|---|---|
| 400 | 请求格式错误 |
| 401 | 未认证 |
| 403 | 权限不足 |
| 404 | 路径不存在 |
| 502 | 后端挂了 / upstream 错 |
| 503 | 限流 / 后端不可用 |
| 504 | 后端超时 |
如果是 proxy_pass:
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
排查:
curl http://127.0.0.1:8080
看后端是否通。
error_log /var/log/nginx/error.log debug;
tail -f /var/log/nginx/error.log
nginx -t 是否通过如果你愿意,可以把 具体报错日志内容 或 nginx 配置片段 发出来,我可以帮你精准定位问题。