Skip to content
Timi Adewumirev 2026.09
go, http, proxies

502 or 504: what a proxy should answer when the upstream fails

A reverse proxy sees eight different upstream failures. Most proxies collapse them into one status code. Here is the mapping I built into edge, and why each row is where it is.

Contents

When an upstream fails, a reverse proxy has to answer the client with something. Most proxies answer 502 Bad Gateway for everything, and the person reading the dashboard at three in the morning learns nothing from it. Was the backend down, slow, or misconfigured? Did the proxy even try?

While building edge, a traffic router in Go, I had to decide this for every failure the transport can report. This article is the table I ended up with, and the reasoning behind each row.

The two status codes mean different things#

502 Bad Gateway means the proxy got an invalid response, or no response, from the upstream. 504 Gateway Timeout means the proxy waited and the upstream did not answer in time. The difference matters operationally:

  • A 502 points at the upstream process or the network path to it. A refused connection, a reset, a certificate that does not verify. Something is broken now.
  • A 504 points at capacity or latency. The upstream exists and accepted the connection, or at least did not refuse it, but it is too slow. Scaling or a slow dependency is the usual cause.

Mixing them hides the first question an on-call engineer asks: is it down, or is it slow?

Name the error type, not only the code#

RFC 9209 defines a Proxy-Status response header. It names the proxy and carries an error parameter from a fixed registry of types: connection_refused, connection_timeout, http_response_timeout, tls_certificate_error, and so on. Envoy and some CDNs set it. A client or a log pipeline can then tell these apart without parsing a body.

HTTP/1.1 502 Bad Gateway
Proxy-Status: edge; error=connection_refused
Content-Type: text/plain; charset=utf-8

Bad Gateway

Two lines in the proxy, and every 502 in the access log now says why.

The mapping#

This is the table edge uses. Each row is a transport error the Go net/http client can return, the status code the client receives, and the Proxy-Status error type.

What the transport reportedStatusProxy-Status error
Dial failed, not a timeout (ECONNREFUSED)502connection_refused
Dial timed out504connection_timeout
Connection reset or closed before the response headers502connection_terminated
Response headers took longer than the header timeout504http_response_timeout
One attempt ran longer than the per-try timeout504http_response_timeout
The whole request, with retries, ran past its timeout504http_response_timeout
Upstream certificate failed verification502tls_certificate_error
TLS handshake error (bad record, alert)502tls_protocol_error
Any other transport error502http_protocol_error

Two more rows happen before any connection is attempted, and they are 503, not 502:

What the pool reportedStatusProxy-Status error
No healthy endpoint in the pool503destination_unavailable
The pool's connection limit is reached503connection_limit_reached

503 Service Unavailable is right here because the proxy itself declined to forward. The client should back off and retry later; nothing about the upstream was tested.

The hard row: a timeout during the dial#

One row needs care. If the per-try timeout fires while the proxy is still dialing, the transport returns a context error, not a dial error. Read naively, that looks like a slow response (http_response_timeout). It is really a slow connect (connection_timeout), which usually means a firewall dropping packets or a dead host, not a slow application.

edge solves this by installing an httptrace.ClientTrace on each attempt and recording whether a connection was ever obtained. When the timeout fires and no connection came, the row is connection_timeout. The code is short:

if errors.Is(cause, ErrPerTryTimeout) || errors.Is(cause, ErrResponseHeaderTimeout) {
    if dial == dialPending {
        return connTimeout // 504, connection_timeout
    }
    return timeout // 504, http_response_timeout
}

The cause is the reason the proxy cancelled the attempt. It comes first because a cancel by the proxy hides the real timeout inside a generic context.Canceled.

Resets have no exported error#

The Go transport has no exported error for "the server closed an idle connection before I could use it". The only test is a string match on the error text. edge checks ECONNRESET, EPIPE, io.EOF, io.ErrUnexpectedEOF, and then the string "server closed idle connection". It is ugly, and it is what net/http/httputil does too.

NOTE

A reset before the response headers is safe to retry: the upstream never saw a complete request, or saw it and never started a response. A reset after the headers is not, because the handler may have run. edge only retries the first case.

Retry with a budget, not a count#

Which rows are retried is a per-route policy: connect-failure, reset, 5xx, gateway-error, or a list of status codes. But a fixed retry count is dangerous under load. If every request retries three times, a struggling upstream receives four times the traffic at the moment it can least afford it.

edge uses a retry budget instead: retries may be at most 20 percent of the active requests on that route, with a floor of three so that a quiet route can retry at all. Two atomic counters implement it with no lock on the hot path.

limit := int64(math.Ceil(float64(b.requests.Load()) * percent / 100))
limit = max(limit, minRetryBudget)
if b.retries.Add(1) > limit {
    b.retries.Add(-1)
    return false
}

This is the same idea as Envoy's retry budget and Finagle's. It is the one retry setting I would copy into any proxy.

What to take from this#

  1. Separate "down" from "slow": 502 for broken, 504 for slow, 503 when the proxy declined to try.
  2. Set Proxy-Status on every error the proxy generates. It costs two lines and it makes every log line self-explaining.
  3. Decide the connect-timeout row explicitly. A timeout while dialing is not a slow response.
  4. Retry from a budget, never from a count.