Nginx 返回 502 Bad Gateway,本质含义是:
Nginx 作为反向代理,无法从上游服务(upstream / 后端)拿到合法的响应
下面按常见原因 → 日志表现 → 排查方法系统说明。
现象
日志示例
connect() failed (111: Connection refused) while connecting to upstream
排查
ps -ef | grep 后端进程
netstat -lntp | grep 端口
现象
日志示例
no live upstreams while connecting to upstream
示例错误配置
proxy_pass http://127.0.0.1:8080; # 实际后端是 9000
现象
日志示例
upstream timed out (110: Connection timed out)
相关参数
proxy_connect_timeout
proxy_read_timeout
proxy_send_timeout
建议
proxy_read_timeout 60s;
现象
日志示例
upstream prematurely closed connection
常见错误
日志示例
recv() failed (104: Connection reset by peer)
/var/log/nginx/error.log
2026/01/15 10:12:33 [error] 1234#0:
*56 connect() failed (111: Connection refused)
while connecting to upstream,
client: 1.2.3.4,
server: example.com,
request: "GET /api HTTP/1.1",
upstream: "http://127.0.0.1:8080/api"
关键信息
connect() failed → 后端连不上timed out → 超时prematurely closed → 后端异常断开✅ 后端是否活着
✅ 端口是否一致
✅ 网络是否通(防火墙 / 容器 / 安全组)
✅ Nginx timeout 是否合理
✅ 协议是否匹配(HTTP / HTTPS / FastCGI)
如果你愿意,可以发:
我可以直接帮你定位是哪一行配置的问题。