HTTP Status Codes 502 vs HTTP Status Codes 504

The difference

Both come from a proxy in front of your app. 502 means the upstream answered badly or refused; 504 means it did not answer in time.

502
HTTP Status Codes · Bad Gateway
504
HTTP Status Codes · Gateway Timeout
MeaningInvalid or no response from upstream (refused, reset)Upstream did not respond before the proxy timeout
Who is wrongUpstream process or network pathUpstream latency vs proxy timeout
Retry?Idempotent requests: once, with backoffIdempotent requests: once; the work may still be running
Typical triggerApp crashed, wrong port, keep-alive raceSlow query, long sync job, saturated workers
FixRestart/health-check upstream, keepAliveTimeout > LB idleSpeed up or queue the work, return 202, tune timeouts
Header to checknginx error.log: connect() failed / upstream prematurely closednginx error.log: upstream timed out (110)

Which one do I have?

  • If it fails instantly → it is 502.
  • If it fails after exactly the proxy timeout (29s, 60s, 100s) → it is 504.
  • If Cloudflare shows 522 or 524 → the same split, at the CDN edge.