Indicates that a server acting as a gateway, CDN, or reverse proxy received an invalid, truncated, or connection-refused response from an upstream server.
Server is temporarily overloaded or undergoing scheduled maintenance. Pair with Retry-After so clients back off cleanly.
Cacheable by Default
No
No
Method Preservation
N/A
N/A
Safe to Auto-Retry
Yes (with backoff)
Yes (with backoff)
Response Body Allowed
Yes
Yes
Key Headers
Cache-Control, Content-Type
Retry-After, Cache-Control
HTTP 502 Bad GatewayContent-Length: 167 BNon-cacheable · Retryable
Fixing HTTP 502 Bad Gateway in Nginx, AWS ALB, and Kubernetes
An HTTP 502 Bad Gateway always means a proxy hop—Nginx, Cloudflare, AWS ALB, or Kubernetes Ingress—tried to talk to an upstream backend and got either a refused TCP connection, a reset socket, or unparseable HTTP framing. Unlike 504 Gateway Timeout (where the upstream simply ran out of time), 502 happens fast, often within milliseconds.
If you see intermittent 502s under light traffic on Node.js or Express behind AWS ALB, check keep-alive timeouts immediately: Node.js defaults to a 5-second keepAliveTimeout while AWS ALB defaults to 60 seconds, creating a classic race condition where ALB sends a request down a socket Node.js is closing. Setting server.keepAliveTimeout = 65000 and server.headersTimeout = 66000 eliminates intermittent ALB 502s completely.
Cacheable / RetryableNot cacheable · Transient 502 is retryable with backoff on idempotent calls
What is the difference between 502 Bad Gateway and 504 Gateway Timeout?
502 means the upstream actively failed, refused the connection, or returned malformed HTTP. 504 means the upstream stayed silent until the proxy read timeout expired.
Why does Node.js behind AWS ALB throw random 502 Bad Gateway errors?
Node.js default keepAliveTimeout (5s) is lower than AWS ALB idle timeout (60s). Raise server.keepAliveTimeout to 65000 ms and headersTimeout to 66000 ms.
How do I fix Nginx "upstream sent too big header" 502 errors?
Increase proxy_buffer_size 16k; and proxy_buffers 4 32k; in Nginx when upstream frameworks emit large OAuth/OIDC Set-Cookie headers.