Quick one — 502, 503 or 504? An nginx reverse proxy with a single upstream server forwards GET /api/orders/42. The backend refuses the TCP connection (nginx logs connect() failed (111: Connection refused) while connecting to upstream). Which status does the client get, and what is that code actually telling you?
- 502 Bad Gateway — the proxy selected an upstream and got an invalid or no response from it; the failure is in that upstream's reply, not in pool capacity and not a timeout. 502 is 'the gateway received a bad answer': refused, reset, closed early, or garbage from the backend it picked.
- 500 Internal Server Error — the proxy reports the backend's crash as a generic server error. 500 is a server's own unhandled error; proxies use the 502/503/504 family precisely so you can tell 'bad reply' from 'nobody available' from 'too slow'.
- 503 Service Unavailable — any backend failure means the service is unavailable. 503 means the proxy could not find a usable upstream at all (every backend unhealthy, queue or connection limit reached, maintenance) or the backend itself answered 503; a bad reply from a selected backend is 502.
- 504 Gateway Timeout — the request could not complete, so it timed out. 504 is reserved for the proxy's upstream timeout expiring with no response; a RST or an immediate close is an instant failure, not a timeout.
nginx returns 502 Bad Gateway: it reached a backend and got a bad or missing reply.
The 5xx family from a proxy encodes where the request died. 502 means the proxy talked (or tried to talk) to a specific backend and the reply was invalid or absent: connection refused, reset, closed before headers, or unparsable. 503 means the proxy could not serve at all — no healthy backend, its own overload or connection limits, or a backend explicitly saying 503. 504 means the proxy's read timeout expired while waiting. Implementations differ at the edges: Envoy returns 503 (response flag UF) for a failed connection and reserves 502 for protocol errors, HAProxy likewise uses 503 for connect failures and 502 for aborted or malformed replies — so read the proxy's error log or response flags rather than trusting the code alone. One nginx detail worth saying out loud: with more than one server in the upstream block, the default proxy_next_upstream error timeout makes nginx try the next server after a refused or prematurely closed connection (not after an invalid header) before giving up, so the client sees 502 only once every server it tried has failed.
Treating 503 as 'the backend is overloaded' and scaling it. From a proxy, 503 most often means the proxy never reached a backend at all — health checks, endpoint discovery or a connection limit — and the backend may never have seen the request.