502 api network_error ai_generated true

502 Bad Gateway: upstream connection reset by peer

ID: api/http-502-bad-gateway-upstream-connection-reset

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
1Evidence
2023-10-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
NGINX 1.24+ active
AWS ALB 2023+ active
HAProxy 2.8+ active
Gunicorn 21+ active
uWSGI 2.0+ active

Root Cause

The upstream server (e.g., application backend) abruptly closed the TCP connection while the proxy (e.g., NGINX, ALB) was trying to forward the request, often due to a crash, timeout, or firewall rule.

generic

中文

上游服务器(例如应用程序后端)在代理(例如 NGINX、ALB)尝试转发请求时突然关闭了 TCP 连接,通常是由于崩溃、超时或防火墙规则。

Official Documentation

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

Workarounds

  1. 85% success Check upstream server logs for application crashes or resource exhaustion. For example, in a Python Gunicorn setup, look for 'Worker failed to boot' or 'Out of memory'. Increase the number of workers or memory limits. Example command to add more workers: gunicorn --workers=4 --timeout=120 myapp:app
    Check upstream server logs for application crashes or resource exhaustion. For example, in a Python Gunicorn setup, look for 'Worker failed to boot' or 'Out of memory'. Increase the number of workers or memory limits. Example command to add more workers:
    gunicorn --workers=4 --timeout=120 myapp:app
  2. 80% success Verify firewall rules between proxy and upstream. On Linux, use iptables to check for dropped packets: iptables -L -n -v | grep DROP If rules are blocking, add an allow rule for the proxy IP on the upstream's port (e.g., port 8080).
    Verify firewall rules between proxy and upstream. On Linux, use iptables to check for dropped packets:
    iptables -L -n -v | grep DROP
    If rules are blocking, add an allow rule for the proxy IP on the upstream's port (e.g., port 8080).

中文步骤

  1. Check upstream server logs for application crashes or resource exhaustion. For example, in a Python Gunicorn setup, look for 'Worker failed to boot' or 'Out of memory'. Increase the number of workers or memory limits. Example command to add more workers:
    gunicorn --workers=4 --timeout=120 myapp:app
  2. Verify firewall rules between proxy and upstream. On Linux, use iptables to check for dropped packets:
    iptables -L -n -v | grep DROP
    If rules are blocking, add an allow rule for the proxy IP on the upstream's port (e.g., port 8080).

Dead Ends

Common approaches that don't work:

  1. 80% fail

    The reset is immediate, not due to a slow response.

  2. 90% fail

    The upstream server is the root cause.