Short answer: 502 is a bad response, 503 is "not now", 504 is "too late"
502 and 504 come from a proxy (CDN, load balancer, nginx) after it passed the request upstream. 503 means nowhere to send it or no room for it, so a proxy or the app can return it. In an incident, find the producing layer first.
Side by side
- 502 Bad Gateway — no usable upstream response · usually instant · refused or dropped connection, crashed worker
- 503 Service Unavailable — cannot handle it now · usually instant, maybe with
Retry-After· no targets, overload, rate limit, maintenance - 504 Gateway Timeout — no upstream answer within the timeout · takes exactly the timeout (say 60 s) · slow queries or external APIs
The same fault can get different codes: a refused connection is a 502 in nginx but a 503 with upstream connect error or disconnect/reset before headers in Envoy (Istio).
How it works in practice: who produced it?
$ curl -sv https://api.example.com/api/orders 2>&1 | grep -E "^< (HTTP|server)"
< HTTP/2 503
< server: nginx
$ curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://api.example.com/reports
504 60.013
- Server header —
awselb/2.0means the ALB generated it. nginx hides the upstream's Server header by default, songinxalone proves nothing. - Body — an HTML page with
<center>nginx</center>is nginx; your app's JSON error format is the app. - Time — exactly a timeout value (60.0 s) means that layer cut it. A 502 or 503 in milliseconds was refused at once or had nowhere to go.
An AWS ALB returns 503 when the target group has no registered targets, 502 when a target resets the connection or sends a malformed response, and 504 past the idle timeout (60 s by default). In its access log, a target_status_code of - means the ALB made it. ingress-nginx returns 503 when no endpoints are ready or a rate limit trips.
When to use which: codes your app returns
- Maintenance or brief overload → 503 with
Retry-After: 120(seconds) - A broken response from an API your app calls → 502; no answer in time → 504
- Your app's own exception → 500; a 502 or 504 from the app looks like a proxy fault.
Failure 1: 503 right after a deploy, zero endpoints
$ kubectl get endpoints checkout -n shop
NAME ENDPOINTS AGE
checkout <none> 41d
# ingress-nginx log
Service "shop/checkout" does not have any active Endpoint.
ingress-nginx had no pod to send to and made the 503 itself, so the app log is quiet. Pods at READY 0/1 mean a failing readiness probe; at 1/1, the Service selector misses the pod labels. Mixed 502s and 504s can mean new pods dying while old ones struggle (simulation: payment API 5xx spike after a deploy).
Failure 2: 504 after exactly 60 seconds
A cut at 60 seconds points to ingress-nginx proxy-read-timeout, nginx proxy_read_timeout or the ALB idle timeout, all 60 s by default. The Server header and body name the layer; the app log gives the real duration. Raise it only for genuinely slow paths, with outer timeouts no shorter than inner ones.
metadata:
annotations:
nginx.ingress.kubernetes.io/proxy-read-timeout: "180"
For slow upstreams and nginx errnos, see the 502 and 504 guide.
Failure 3: every request returns 502 after an upgrade
# ingress-nginx log
connect() failed (111: Connection refused) while connecting to upstream, upstream: "http://10.42.1.9:8080/api/orders"
# app log
INFO server listening on 127.0.0.1:8080
The pod IP is in the endpoints, so it is not a 503; the connection to it is refused, so it is a 502. The new version listens only on 127.0.0.1, not the pod IP. Make it listen on 0.0.0.0 and check the Service targetPort. A readiness probe on that port would have kept the pods unready, turning this into a 503.
What not to do
- Raising timeouts for every 5xx — it only helps a 504.
- Removing the readiness probe to stop 503s — unready pods get traffic and you get 502s.
- Retrying 5xx without backoff — it deepens the overload.
The order to work in
- Read the code, Server header, body and time with
curl - 503 → anything to send to (endpoints, target group)?
- 502 → does the app listen where the proxy connects?
- 504 → which layer cut it, and why is the upstream slow?
For a 503 with healthy-looking pods, try A pod is Running and one Service still has no endpoints.
Quick diagnostic checklist
- Use
curl -svto read the status code and theserver:header and see which layer answered. server: awselb/2.0means the ALB produced it; check whethertarget_status_codeis-in the access log.server: nginxcan still be an app response, so check whether the body is nginx's default HTML or your app's JSON.- Time the request with
curl -w "%{http_code} %{time_total}"and compare it with timeouts such as 60 seconds. - For a 503 behind an ingress, check whether
kubectl get endpointsshows<none>. - With no endpoints, check pod READY (0/1 means a failing readiness probe) and compare the Service selector with the pod labels.
- For a 502, take the upstream address from the proxy error log and confirm the app listens on that port on
0.0.0.0. - For a 504, read the real processing time in the app log and raise timeouts only for slow paths, keeping outer layers no shorter.