Nginx 出现 502 Bad Gateway 通常表示:Nginx 作为反向代理,无法从上游服务(如 PHP-FPM、Tomcat、Node、Python 等)拿到合法响应。排查可以从“Nginx → 上游服务”这条链路入手。
下面按最常见 → 较少见的顺序给你一套排查思路。
tail -f /var/log/nginx/error.log
常见 502 相关错误示例:
connect() failed (111: Connection refused) while connecting to upstream
✅ 说明:
排查:
netstat -lntp | grep 端口
# 或
ss -lntp | grep 端口
upstream timed out (110: Connection timed out)
✅ 说明:
排查:
recv() failed (104: Connection reset by peer)
✅ 说明:
too many open files
✅ 说明:
curl http://127.0.0.1:端口/接口
常见错误配置示例:
location / {
proxy_pass http://127.0.0.1:8080;
}
✅ 检查点:
/ 导致路径问题如果是 PHP:
connect() to unix:/run/php-fpm.sock failed (2: No such file or directory)
排查:
ps aux | grep php-fpm
ls -l /run/php-fpm.sock
✅ 解决:
fastcgi_pass可在 Nginx 增加超时时间(临时验证):
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
⚠️ 不推荐长期靠这个解决,应优化后端。
free -h
top
df -h
ulimit -n
常见问题:
如果你愿意,可以把:
发出来,我可以直接帮你定位具体原因。