温馨提示×

nginx日志报错如何排查

小樊
33
2026-09-08 15:04:30
栏目: 编程语言

Nginx 日志报错排查,一般可以按下面思路一步步来:


一、先搞清楚 Nginx 有哪些日志

Nginx 主要有两类日志:

  1. 访问日志(access.log)

    • 记录每次请求情况
    • 路径一般在:
      /var/log/nginx/access.log
      
  2. 错误日志(error.log)

    • 记录启动、配置、运行时错误
    • 路径一般在:
      /var/log/nginx/error.log
      

查看日志路径配置:

nginx -T | grep -E "access_log|error_log"

二、常见排查步骤

1️⃣ 看 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

    • root 或 proxy_pass 路径错误
  • Address already in use

    • 端口被占用
    • 解决:
      netstat -tunlp | grep 80
      
  • unknown directive

    • 配置文件语法错误

2️⃣ 检查 Nginx 配置是否正确

nginx -t

如果有错误,会直接指出文件和行号。


3️⃣ 看 access.log 辅助分析

tail -n 50 /var/log/nginx/access.log

关注:

  • 状态码(4xx / 5xx)
  • 请求 URL
  • 客户端 IP

例如:

GET /api/test 502

说明后端服务有问题


4️⃣ 常见错误 & 对应原因

状态码 常见原因
400 请求格式错误
401 未认证
403 权限不足
404 路径不存在
502 后端挂了 / upstream 错
503 限流 / 后端不可用
504 后端超时

5️⃣ 反向代理 / 负载均衡问题

如果是 proxy_pass

location /api/ {
    proxy_pass http://127.0.0.1:8080;
}

排查:

curl http://127.0.0.1:8080

看后端是否通。


6️⃣ 动态调试技巧

  • 提高日志级别:
error_log /var/log/nginx/error.log debug;
  • 实时看日志:
tail -f /var/log/nginx/error.log

三、快速排查 checklist ✅

  • [ ] nginx -t 是否通过
  • [ ] error.log 有没有明显报错
  • [ ] 文件/目录权限是否正确
  • [ ] 端口是否被占用
  • [ ] 后端服务是否正常
  • [ ] 防火墙 / SELinux 是否拦截

如果你愿意,可以把 具体报错日志内容nginx 配置片段 发出来,我可以帮你精准定位问题。

0