温馨提示×

nginx日志为何频繁报错

小樊
34
2026-09-04 11:00:42
栏目: 编程语言

nginx 日志频繁报错的原因有很多,需要先看具体报错内容才能准确判断。下面我按「常见报错类型 → 可能原因 → 排查/解决办法」给你一个系统性的梳理,你可以对照自己的日志。


一、最常见几类 nginx 报错

1️⃣ 502 Bad Gateway

日志常见:

connect() failed (111: Connection refused) while connecting to upstream

可能原因:

  • 后端服务(PHP-FPM / Java / Node)挂了
  • upstream 配置错误(端口、地址不对)
  • 后端响应太慢,被 nginx 超时切断

排查:

ps aux | grep php-fpm
netstat -lntp | grep 9000
curl http://127.0.0.1:端口

2️⃣ 504 Gateway Time-out

日志常见:

upstream timed out (110: Connection timed out)

原因:

  • 后端处理时间 > nginx proxy_read_timeout
  • 数据库慢查询
  • 网络延迟高

解决:

proxy_read_timeout 60s;
proxy_connect_timeout 10s;

3️⃣ 403 Forbidden

日志常见:

directory index of "/path/" is forbidden

原因:

  • 没有 index 文件
  • 文件权限不对(www-data / nginx 用户无权限)
  • autoindex off

检查:

ls -l /var/www/html
ps aux | grep nginx

4️⃣ 404 Not Found

原因:

  • location 匹配错误
  • root / alias 配置不对
  • rewrite 规则问题

注意:

  • rootalias 混用是高频坑

5️⃣ Too many open files

日志常见:

worker_connections are not enough

原因:

  • 并发高
  • 系统 ulimit 限制

解决:

ulimit -n 65535
worker_rlimit_nofile 65535;

6️⃣ SSL 相关报错

SSL_do_handshake() failed
  • 证书过期
  • 协议不匹配(TLS 版本)
  • 客户端太老

二、你可以这样快速定位

1️⃣ 看错误日志

tail -f /var/log/nginx/error.log

2️⃣ 看访问日志状态码

awk '{print $9}' access.log | sort | uniq -c | sort -rn

3️⃣ 检查配置

nginx -t

三、如果你愿意,我可以直接帮你分析

你可以把具体的错误日志内容(哪怕几行)发出来,例如:

  • 报错时间
  • 错误类型
  • 对应 location / upstream 配置

我可以直接告诉你是哪一行的配置问题,以及怎么改

0