T The Triage ManualTechnical Guides for IT Emergencies
P2 · Network Infrastructure

HTTP 502 Bad Gateway — Upstream Server Connectivity Failure (Nginx / HAProxy / ALB / IIS ARR)

An HTTP 502 Bad Gateway error is returned when a reverse proxy, load balancer, or API gateway receives an invalid or no response from an upstream backend server. Root causes include the backend process being stopped or crashed, network/firewall blocking gateway-to-backend traffic, misconfigured upstream address or port in the proxy configuration, or the backend being overloaded and dropping connections before sending a valid HTTP response. Resolution requires identifying whether the upstream is down, unreachable, or misconfigured, then restoring the backend service or correcting the proxy configuration. This error is distinct from HTTP 503 (service unavailable) and HTTP 504 (gateway timeout) and demands upstream-side investigation, not client-side remediation.

Indicators

Likely causes

Diagnostic steps

  1. Check the gateway/proxy error log for the 502 event to identify which upstream host and port is failing and the exact error message. For Nginx: `tail -100 /var/log/nginx/error.log | grep 502`. Look for messages such as 'connect() failed', 'upstream prematurely closed connection', or 'no live upstreams while connecting to upstream'.
    Pinpoints which backend target the proxy is blaming and the nature of the failure (connection refused, connection timeout, bad response), narrowing the investigation before touching the backend.
  2. From the gateway server, test direct TCP connectivity to the upstream backend on the expected port: `curl -v http://<backend-host>:<port>/healthcheck` or `telnet <backend-host> <port>` or `nc -zv <backend-host> <port>`.
    Determines whether the gateway can reach the backend at the network and TCP level, ruling in or out firewall/routing issues independently of the application layer.
  3. On the upstream backend server, verify that the application process is running and listening: Linux: `systemctl status <service-name>` and `ss -tlnp | grep <port>`. Windows: `Get-Service -Name <ServiceName>` and `netstat -ano | findstr <port>`. Review application logs for crashes, OOM events, or startup errors.
    Confirms whether the backend service is running and bound to the expected port, or has crashed/stopped — the most common cause of 502 errors.
  4. Review the proxy/gateway configuration to verify the upstream address, port, and protocol are correctly specified and match the actual backend listener. For Nginx: `cat /etc/nginx/nginx.conf` (or the relevant server block). For HAProxy: `cat /etc/haproxy/haproxy.cfg`. Validate config syntax before reloading: `nginx -t` or `haproxy -c -f /etc/haproxy/haproxy.cfg`.
    Rules out configuration drift or deployment errors as the cause of the 502, especially after a recent infrastructure change, server rename, or migration.
  5. Check backend server resource utilisation for signs of exhaustion: `top` or `htop` (CPU/memory), `vmstat 1 5` (memory pressure, swap), `netstat -an | grep <port> | wc -l` or `ss -s` (connection counts), `ulimit -n` and `cat /proc/<pid>/fd | wc -l` (file descriptor limits). On Windows: Task Manager or `Get-Process | Sort-Object CPU -Descending | Select-Object -First 10`.
    Identifies if the backend process is alive but overwhelmed — saturated CPU, exhausted worker threads, or file descriptor limits — causing connections to be dropped before a valid HTTP response is formed.

Resolution path

Prevention

Tools

References

http-502bad-gatewayreverse-proxyload-balancerupstream-failurenginxhaproxyiis-arrapi-gatewayweb-serverconnectivityincident-responsealbcloudflare