排查 502 Bad Gateway 的核心思路是:先定位是网关/代理层问题,还是上游服务问题。下面按“由外到内、由快到准”的顺序给你一套通用排查流程,适用于 Nginx、CDN、K8s Ingress、API Gateway 等常见场景。
502 表示:
网关/代理服务器 能收到请求,但上游服务返回了无效响应或没响应
常见链路:
客户端 → 网关(Nginx/CDN/Ingress) → 上游服务(Java/Go/Node/PHP)
浏览器 DevTools / curl:
curl -I https://example.com
常见特征:
Server: nginx → Nginx 返回Via: 1.1 vegur → CDN / 云厂商X-Cache: Error → CDN 问题✅ 结论:
tail -f /var/log/nginx/error.log
常见错误:
connect() failed (111: Connection refused)upstream timed outno live upstreamsrecv() failed (104: Connection reset by peer)✅ 说明:
upstream backend {
server 127.0.0.1:8080;
}
确认:
ss -lntp | grep 8080
netstat -tulnp | grep 8080
curl http://127.0.0.1:8080/health
ps -ef | grep java
systemctl status your-service
ss -lntp
查看:
journalctl -u your-service -n 100
常见原因:
proxy_connect_timeout 5s;
proxy_read_timeout 30s;
proxy_send_timeout 30s;
✅ 排查:
kubectl get pods
kubectl describe pod xxx
kubectl get svc
kubectl get ingress
kubectl get endpoints
✅ 常见原因:
✅ 排查:
✅ 5 分钟定位法:
curl 上游服务| 原因 | 特征 |
|---|---|
| 服务没启动 | connection refused |
| 服务崩了 | connection reset |
| 超时 | upstream timed out |
| 容器没 ready | no live upstreams |
| 端口配错 | connect failed |
| 防火墙 | timeout / reset |
你可以直接贴:
我可以 直接帮你定位到具体配置或代码问题。