HTTP 502 Bad Gateway Status Code

5xx Server Error · RFC 9110 §15.6.3 · Reverse proxy received an invalid or broken response from upstream

502 Bad Gateway RFC 9110 §15.6.3
Open /502/ →
Response & Header Builder
1m
max-age: 0s (immediate)
Editable Response Body
Live Raw HTTP Response 6 headers · 167 bytes
HTTP/1.1 502 Bad Gateway
Date: Tue, 24 Nov 2026 12:00:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 167
Cache-Control: no-store
Content-Type: application/problem+json; charset=utf-8
X-Content-Type-Options: nosniff

{"type":"https://bytehelpers.com/http-status/502/","title":"Bad Gateway","status":502,"detail":"Reverse proxy received an invalid response from the upstream service."}
location /api/example {
    add_header Cache-Control "no-store" always;
    add_header X-Content-Type-Options "nosniff" always;
    default_type application/json;
    return 502 '{"type":"https://bytehelpers.com/http-status/502/","title":"Bad Gateway","status":502,"detail":"Reverse proxy received an invalid response from the upstream service."}';
}
Pairwise HTTP Confusion Comparator
Open /http-status/503/ →
Attribute 502 Bad Gateway 503 Service Unavailable
Status & Phrase502 Bad Gateway503 Service Unavailable
Category5xx Server Error5xx Server Error
SpecificationRFC 9110 §15.6.3RFC 9110 §15.6.4
Core SemanticsIndicates 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 DefaultNoNo
Method PreservationN/AN/A
Safe to Auto-RetryYes (with backoff)Yes (with backoff)
Response Body AllowedYesYes
Key HeadersCache-Control, Content-TypeRetry-After, Cache-Control
HTTP 502 Bad Gateway Content-Length: 167 B Non-cacheable · Retryable
Ctrl+Enter: Copy Raw · 100% Browser-Local
Advertisement
Reserved fixed-height slot (CLS = 0.00) 728×90 320×100 300×50

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.

  • RFC SpecificationRFC 9110 §15.6.3 (Server Error 5xx)
  • Emitted ByReverse proxy / Load Balancer / CDN (Nginx, AWS ALB, Envoy, Cloudflare)
  • 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.