HTTP 502 Bad Gateway — What It Means & How to Debug
HTTP 502 Bad Gateway is an HTTP response status code that indicates a server acting as a gateway or proxy received an invalid response from an upstream server it accessed while fulfilling the request. The 502 is one of the most common reverse-proxy errors, appearing when Nginx, a CDN, or an API gateway cannot get a valid response from the backend application server.
What It Means
Defined in RFC 7231 Section 6.6.3, the 502 status code signals a breakdown in server-to-server communication. The gateway or proxy successfully established a TCP connection to the upstream server and sent the request, but the response it received was malformed, incomplete, or otherwise invalid. The proxy cannot forward a response it cannot parse.
When It’s Sent
- Upstream server crash — The application server (e.g., uWSGI, Gunicorn, Puma) crashed or was killed after the proxy forwarded the request.
- Upstream returns garbled data — The backend responds with non-HTTP content, binary garbage, or an unparseable response body.
- SSL/TLS handshake failure — The proxy cannot establish a secure connection to the upstream.
- Upstream protocol mismatch — The proxy speaks HTTP/1.1 but the upstream speaks only HTTP/2, or vice versa.
- Upstream timeout with partial response — The backend begins sending a response but the connection drops before the headers or body are complete.
- CDN origin unreachable — A CDN edge node cannot fetch content from the origin server.
Real Example
The following curl command hits an endpoint configured with a broken upstream:
curl -v https://httpbin.org/status/502Expected response:
< HTTP/1.1 502 BAD GATEWAY
< Date: Sat, 20 Jun 2026 12:00:00 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 184
< Server: gunicorn/19.9.0
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>502 Bad Gateway</title>
<h1>Bad Gateway</h1>
<p>The server returned an invalid or incomplete response.</p>A real-world Nginx 502 with a broken upstream might look like:
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>How to Debug
Client-Side
- Retry after a short delay — A 502 is often transient (upstream restart, deployment). Retry with backoff.
- Check the proxy service status — If you are hitting a CDN (Cloudflare, Akamai) or API gateway, check that service’s status page for outages.
- Try direct access — If you know the upstream server’s address, bypass the proxy and see if the backend responds directly.
- Clear DNS cache — A stale DNS entry might point to a dead backend the proxy cannot reach.
Server-Side
- Check upstream health — Verify the application server is running:
ps aux | grep gunicornor check systemd status. - Review upstream logs — The application server logs often have the real error. Look for crashes, timeouts, or out-of-memory events.
- Check proxy error logs — Nginx logs
upstream prematurely closed connectionorupstream sent no valid HTTP response. Apache logs(20014)Internal errorin the error log. - Inspect upstream port and socket — Make sure the proxy is pointing to the correct port or Unix socket. A common mistake is a misaligned port after a deploy.
- Verify upstream returns valid HTTP — Use
curldirectly against the upstream to confirm it returns proper HTTP with complete headers. - Check for resource exhaustion — If the upstream runs out of file descriptors, memory, or worker threads, it may drop connections mid-response.
Common Causes Table
| Scenario | Likely Cause | How to Fix |
|---|---|---|
| App server restarted during deploy | Brief window with no running workers | Use zero-downtime deploys with rolling restarts |
| Nginx returns 502 after deploy | Upstream bind port changed | Update proxy_pass to the correct port/socket |
| CDN returns 502 on origin pull | Origin server is down or overloaded | Scale origin; enable CDN origin shielding |
| SSL handshake failure | Upstream certificate expired or self-signed | Renew certificate; configure proxy to trust upstream CA |
| Upstream returns HTTP/0.9 response | Backend sends raw data without HTTP headers | Ensure backend returns proper HTTP response |
FAQ
Related Codes
HTTP 504 Gateway Timeout — The upstream server did not respond in time.
HTTP 503 Service Unavailable — The server is temporarily unable to handle the request.
HTTP 500 Internal Server Error — A generic server-side error.
HTTP 502 Bad Gateway — The gateway received an invalid upstream response.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro