HTTP Status Codes
HTTP 502 Bad Gateway
Bad Gateway — the server received an invalid response from an upstream server
What 502 Means
The 502 error on the HTTP Status Codes indicates bad gateway — the server received an invalid response from an upstream server. This typically occurs due to upstream application server is down or not responding.
A 502 Bad Gateway error occurs at the network layer between a reverse proxy or gateway and the upstream application server. The proxy successfully received the client request but got an invalid or no response from the backend. This is defined in RFC 7231 Section 6.6.3 and is specific to multi-server architectures where a gateway or proxy mediates between the client and the origin server. It is one of the most common errors in modern cloud and containerized deployments.
Technical Background
A 502 Bad Gateway is inherently a multi-server error. It only occurs in architectures where one server (the gateway or proxy) forwards requests to another server (the upstream or origin). The gateway is working correctly — it received the client request and attempted to pass it along — but the upstream server either returned an invalid response, closed the connection prematurely, or did not respond at all.
The distinction between 502 and 504 is timing. A 502 means the gateway received a response from the upstream server, but the response was malformed or invalid. A 504 means the gateway never received any response within its configured timeout period. In practice, many proxy servers use 502 for both scenarios when the upstream connection fails unexpectedly.
In containerized and microservice environments, 502 errors frequently occur during deployments when old application instances are terminated before new ones are ready to accept connections. Health checks and graceful shutdown procedures exist specifically to minimize this window of unavailability.
Common Causes
- Upstream application server is down or not responding
- Reverse proxy (Nginx/Apache) timeout while waiting for the application
- Firewall blocking communication between proxy and application
- DNS resolution failure for the upstream server
Typical Scenarios
- An Nginx reverse proxy returns 502 because the upstream Node.js application crashed
- A load balancer cannot reach any healthy backend instances after a deployment
- A CDN edge server times out waiting for the origin server to respond
What to Know
A 502 error usually indicates a problem between servers rather than an issue with a specific URL or page. If one page returns 502 while others work, it may point to a specific upstream service that handles that route. When all pages return 502, the entire upstream application is likely down or unreachable. These errors are often transient during deployments and may resolve within seconds to minutes as new application instances come online.
Frequently Asked Questions
Common questions about HTTP 502 error
A 500 error means your application itself crashed. A 502 error means your reverse proxy (like Nginx) could not get a valid response from the application server behind it — often because the app is down or took too long to respond.
A 502 Bad Gateway means the proxy received an invalid response from the upstream server. A 504 Gateway Timeout means the proxy received no response at all within its timeout window. Both indicate upstream communication problems, but 504 specifically points to a timeout.
During a deployment, old application instances are shut down and new ones start up. If traffic reaches the proxy during this transition — after old instances stop but before new ones are ready — the proxy has no valid upstream to forward to and returns 502.