HTTP 502 Bad Gateway
The middleman's complaint: a proxy or CDN asked the real server and got garbage — or nothing — back.
What HTTP 502 means
HTTP 502 Bad Gateway comes from an intermediary — nginx, a load balancer, Cloudflare — that forwarded your request to an upstream server and received an invalid response: a connection refused, a crash mid-response, or bytes that do not parse as HTTP.
The named culprit is almost never the proxy itself; it is the application server behind it being down, overloaded or unreachable.
Common causes of 502 errors
- The application server (PHP-FPM, Node, gunicorn, etc.) crashed or is not running.
- The proxy points at the wrong port/socket, or a firewall blocks the upstream connection.
- Upstream response exceeded buffers or timed out mid-transfer.
- A deploy restarted backends while traffic was flowing.
- With CDNs like Cloudflare: the origin server is down while the edge is fine.
How to fix it as a visitor
- Reload after a minute — 502s during deploys resolve themselves.
- Check the service's status page if it keeps happening.
How to fix it as a developer
- Check whether the upstream process is alive and listening on the address the proxy expects.
- Read the proxy's error log — nginx says exactly why (connect() failed, upstream prematurely closed, etc.).
- Verify ports, unix socket paths and firewall rules between proxy and app.
- Add health checks so the load balancer stops routing to dead instances.
Example response
HTTP/1.1 502 Bad Gateway Server: nginx Content-Type: text/html <html><body><h1>502 Bad Gateway</h1></body></html>
FAQ
What is the difference between 502 and 504?
502 means the upstream answered with something invalid (or refused the connection); 504 means it did not answer within the timeout at all.
Why does Cloudflare show 502?
Cloudflare's edge could not get a valid response from your origin server — the origin is down, unreachable or returning malformed responses.
Is a 502 temporary?
Usually yes — deploys and restarts cause brief 502s. Persistent 502s mean the backend is genuinely down or misconfigured.