- Home
- HTTP Status Codes
- HTTP 502 Bad Gateway: Server Communication Error
Status Code
HTTP 502 Bad Gateway: Server Communication Error
Learn what 502 Bad Gateway means, why it happens, and how to fix it. Guide covering proxy errors, upstream failures, and troubleshooting.
TL;DR: A 502 usually means your edge server is alive, but the service behind it answered in a way the edge could not use.
What A 502 Is Really Telling You
502 Bad Gateway is a proxy-layer error. The browser reached nginx, Cloudflare, a load balancer, or another gateway successfully. That layer then tried to talk to an upstream app and got back something broken, incomplete, or impossible to parse.
Client -> Proxy or CDN -> App server
\-> invalid upstream response -> 502
That is why a 502 often feels confusing: the system that returns the error is not always the system that caused it.
The Most Common Real Causes
1. The upstream is not actually listening
The proxy points at 127.0.0.1:3000, but the app crashed, moved ports, or never started.
2. The upstream closed the connection too early
The app accepted the request, then died, crashed, or reset the socket before finishing the HTTP response.
3. The upstream returned something malformed
Examples:
- invalid headers
- partial response bytes
- HTTP spoken on a port expecting FastCGI or gRPC
- TLS expected on one side but plain HTTP sent on the other
4. Connection reuse is misconfigured
Under load, keep-alive mismatches can create intermittent 502s that disappear on refresh and only show up in bursts.
Start The Investigation Here
Check the proxy error log before the app log. The proxy usually tells you what kind of bad upstream behavior it saw.
Typical clues:
connection refusedupstream prematurely closed connectionupstream sent invalid headerrecv() failed
Those are much more actionable than the browser’s generic “502 Bad Gateway.”
A Good Debugging Order
- Confirm which proxy layer emitted the 502.
- Check that layer’s error log.
- Hit the upstream directly with
curl. - Confirm the upstream process, port, and protocol.
- Compare proxy and upstream timeout and keep-alive settings.
Direct Checks
# Is the app running where the proxy expects it?
curl -v http://127.0.0.1:3000/health
# Is the service process healthy?
systemctl status your-app
# What is nginx complaining about?
tail -f /var/log/nginx/error.log
If the app answers directly but the proxy still returns 502, the issue is often protocol mismatch, header formatting, or connection reuse rather than pure app downtime.
502 vs 503 vs 504
| Code | What it usually means |
|---|---|
| 502 | Upstream answered incorrectly |
| 503 | Service is unavailable or intentionally shedding work |
| 504 | Upstream did not answer before timeout |
That distinction matters for retries. A flaky 502 often points to integration or transport problems. A 503 often points to capacity or maintenance. A 504 points to slowness.
The Practical Fix Mindset
Do not treat 502 as “the server is down.” Treat it as “a boundary between two services is unhealthy.” That framing gets you to the right logs faster.
Related
Frequently Asked Questions
What does 502 Bad Gateway mean?
A 502 means a gateway or reverse proxy could talk to the client, but got an invalid response from the upstream service behind it.
What causes 502 Bad Gateway?
Common causes include an upstream crash, the wrong upstream port, invalid upstream headers, connection resets, or keep-alive mismatches between layers.
How do I fix a 502 error?
Check the proxy error log first, then verify the upstream process, health endpoint, network path, and timeout settings. The exact fix depends on why the proxy considered the upstream response invalid.
What is the difference between 502 and 504?
502 means the upstream answered badly. 504 means the upstream did not answer in time.