HTTP status · 5xx — server error
502 Bad Gateway
502 Bad Gateway means a gateway or proxy received an invalid response from the upstream server.
Key facts
| Class | 5xx — server error |
|---|---|
| Standard | RFC 9110 §15.6.3 |
| Cacheable by default | No |
| Cause | Invalid response from upstream |
| Typical layer | Reverse proxy, CDN, load balancer |
What 502 means
While acting as a gateway or proxy, the server received an invalid response from an upstream it needed to complete the request. The upstream may have crashed, closed the connection mid-response, spoken the wrong protocol, or sent malformed headers. The client's request was fine; the failure is between two servers.
Common causes
- The backend process is down or restarting, so the proxy's connection is refused.
- The proxy points at the wrong port or host, or at a TLS endpoint without TLS configured.
- The upstream returns a response the proxy cannot parse, such as a truncated body or invalid header.
- The backend exceeds a proxy buffer or timeout limit and the connection is cut.
Check proxy error logs first — they usually name the upstream and the reason. A 502 that appears right after a deploy often means the app has not finished starting or a health check is failing.
Frequently asked questions
What is the difference between 502 and 504?
502 means the upstream returned an invalid response. 504 means the upstream did not respond in time.
Is 502 caused by the client?
Usually not. It is a server-to-server failure between a proxy and its upstream, though an oversized or malformed request can sometimes trigger a proxy error.