Skip to content

502 Bad Gateway — Meaning, Common Causes & How to Fix

502 5xx Server Error RFC 9110 §15.6.3

502 Bad Gateway means: A server acting as gateway or proxy received an invalid response from the upstream it consulted. The edge is alive but the origin (or next hop) answered with garbage, reset the connection, or spoke a protocol the gateway could not parse. Server-error 5xx codes mean the server failed while trying to fulfill a valid request. The fault lies with the service or its upstreams, not the caller. Some members are transient and retryable with backoff; others indicate bugs or configuration errors that only the operator can fix. In practice you will meet 502 when the origin process crashed or restarted mid-request (connection reset); an upstream returned a malformed response or violated http framing; a misconfigured proxy_pass / upstream address pointing at the wrong service. On the client side, retry with backoff — many 502s are transient (deploys, restarts). If persistent, the operator must fix the upstream; nothing in your request will change it. On the server side, check upstream health, error logs and proxy configuration; align keep-alive timeouts between LB and origin (a classic cause of sporadic 502s during deploys).

Advertisement
Ad slot · ref-top

When 502 happens

  • The origin process crashed or restarted mid-request (connection reset)
  • An upstream returned a malformed response or violated HTTP framing
  • A misconfigured proxy_pass / upstream address pointing at the wrong service

How to fix it

On the client

Retry with backoff — many 502s are transient (deploys, restarts). If persistent, the operator must fix the upstream; nothing in your request will change it.

On the server

Check upstream health, error logs and proxy configuration; align keep-alive timeouts between LB and origin (a classic cause of sporadic 502s during deploys).

Should you retry?

Retryable with backoff — often a deploy/restart blip; escalate if persistent.

Debugging steps

When 502 appears unexpectedly, start by capturing the full exchange: 'curl -i' or the browser DevTools Network tab shows the exact status line and response headers, which often carry the real explanation. Determine whether the error came from the edge or the origin: a proxy-generated error page looks different from your app's. Then trace upstream health, timeouts and recent deployments. For this code specifically, check whether the most common trigger applies to your situation: the origin process crashed or restarted mid-request (connection reset). Compare your exchange against the raw HTTP example above. If you operate the server, correlate the timestamp with application and proxy logs using the request id; if you are the client, verify the request shape against the API documentation before changing anything or retrying.

Notes & gotchas

Sporadic 502s during deploys usually mean keep-alive mismatch: the load balancer forwards into a socket the dying origin already closed. Graceful shutdown plus LB drain delays eliminates them.

Examples

Raw HTTP
HTTP/1.1 502 Bad Gateway
Content-Type: application/json
Content-Length: 115

{
  "error": "bad_gateway",
  "message": "The origin process crashed or restarted mid-request (connection reset)"
}
curl
curl -i https://api.example.com/v1/resource   # observe the 502 response and its headers
JavaScript (fetch)
const res = await fetch("https://api.example.com/v1/resource");
if (res.status === 502) {
  // server-side failure: retry idempotent calls with backoff + jitter,
  // surface a friendly error after the retry budget is exhausted
  throw new TransientError(await res.text());
}
Python (requests)
import requests

res = requests.get("https://api.example.com/v1/resource", timeout=10)
if res.status_code == 502:
    # server fault: back off and retry idempotent calls;
    # log res.headers.get("X-Request-Id") for the operator
    res.raise_for_status()

Frequently asked questions

Why do 502s spike during deployments?

Rolling restarts close connections the LB still considers alive; the LB forwards a request into a dying socket and gets a reset. Fix with graceful shutdown and LB drain delays.

502 vs 504: what is the difference?

502: the upstream answered, but invalidly (or reset). 504: the upstream did not answer in time at all.

Are 5xx errors safe to retry?

Idempotent requests (GET, PUT, DELETE) generally yes, with exponential backoff and jitter. Non-idempotent POSTs need care: a 504 may mean the work completed upstream. Check state or use idempotency keys before retrying.

Who should fix a 5xx — the caller or the provider?

The provider: 5xx asserts a server-side fault. Callers can only retry politely (backoff, idempotency) and report correlation ids; persistent 5xx need operator action on logs, upstreams or configuration.

Advertisement
Ad slot · ref-in-article

Try the JSON Validator

Check JSON syntax and locate errors by line and column.

Open tool →