
A 502 Bad Gateway error means a server acting as a gateway or proxy received an invalid response from an upstream server it was trying to reach. It signals a communication failure between servers, not a problem with the client’s request. The error is returned by the intermediary — such as a CDN or load balancer — not the origin server itself.
Common causes include upstream server failures or crashes, server overload during traffic spikes, misconfigured firewalls blocking legitimate responses, and timeout issues when waiting for an upstream response. Network connectivity problems between servers in the request chain can also trigger a 502. Because requests often flow through multiple layers — CDN, load balancer, application server, external API — a breakdown at any link can produce this error.
As a developer or DevOps engineer, start by checking the upstream service’s status page and reviewing application and server logs to identify where the failure occurred. Test the upstream endpoint directly using tools like curl or Postman, and audit your firewall and gateway configurations. If you are an end user, refreshing the page, clearing your browser cache, or trying a different network may resolve a transient 502.
Both codes originate at a gateway or proxy, but they describe different failure modes. A 502 Bad Gateway means the upstream server responded with something invalid or malformed. A 504 Gateway Timeout means the upstream server did not respond within the allowed time — so 502 points to a bad response, while 504 points to no response.
A 500 error indicates something went wrong within the server handling your request directly. A 502 is more specific: it tells you the problem occurred at a gateway or proxy that was relaying your request to another server. The 502 code pinpoints the failure to the communication between servers rather than a general internal fault.
502 errors are often temporary, caused by short-lived conditions such as a traffic spike overloading an upstream server or a brief network disruption between servers. Retrying the request after a short wait frequently resolves the issue. Persistent 502 errors, however, indicate a deeper problem — such as a misconfigured proxy, a crashed upstream process, or a blocked network path — that requires investigation.