Skip to content
5xx · server errorSent by Proxy | CDN

504Gateway Timeout

A proxy passed your request on, waited, and gave up before the server behind it answered.

How you get a 504

The database is stuck on a slow query. The proxy forwards the request and starts its timer. Sixty seconds pass with no reply, so the proxy gives up and sends 504.

Who sends itProxy · CDN

  1. Browser
  2. CDN
  3. Proxy
  4. App
  5. DB

Typically the reverse proxy, load balancer or CDN. The app does not send it, and is usually still working on the request when the 504 goes out.

In short

What a 504 Gateway Timeout means

A 504 Gateway Timeout is written by a middle box that ran out of patience. A reverse proxy, load balancer or CDN forwarded the request, started a timer, and heard nothing back before the timer ended. nginx waits 60 seconds by default; other products have their own limits. The key fact is what a 504 does not tell you: it does not say the work failed. The application is often still busy with a slow query or a slow call to another service, and may finish successfully a moment after the proxy has already answered. That makes blind retries risky, because the same order or payment can be carried out twice. The tell-tale sign is timing. Every failure takes almost exactly the proxy's timeout, to the second.

Common causes of a 504

  1. 1A slow database query or a lock holds the request longer than the proxy will wait.

    How to check: The proxy's error log shows a read timeout; the database shows a long-running statement.

    upstream timed out (110: Connection timed out) while reading response header from upstream
  2. 2The app is waiting on another service that is slow, and has no timeout of its own on that call.

    How to check: Compare outbound call durations in the app's traces or logs with the proxy timeout.

  3. 3All workers or database connections are in use, so the request waits in a queue inside the app.

    How to check: Look at worker and connection pool saturation at the time of the errors, not at CPU.

  4. 4The job is legitimately long (an export, a report) and the proxy timeout is shorter than it.

    How to check: Failures take exactly the configured limit. In nginx that is proxy_read_timeout, 60s unless changed.

    proxy_read_timeout 60s;
  5. 5The network silently drops packets to the upstream (firewall or security group), so the connection never opens.

    How to check: The proxy log says the timeout happened while connecting, not while reading.

    upstream timed out (110: Connection timed out) while connecting to upstream

Retry or not?

Safe to retry with backoff · Idempotent requests only

A 504 does not mean the work stopped. The app may still finish it, so repeating a payment or an order can do it twice. Retry reads freely with backoff. Retry writes only if the request carries an idempotency key or is naturally safe to repeat.

Headers that matter

Server / Via
Shows which hop's timer fired. Each proxy in the chain has its own, and the shortest one wins.
X-Request-Id
A common convention, not part of the standard. Use it to find the same request in the app's logs and see whether it finished.
Idempotency-Key
A request header by convention, not part of RFC 9110. It lets the server recognise a retry and avoid doing the work twice.

In the real world

RFC vs real deployments

RFC 9110 describes 504 as a gateway not getting a timely response from the server it needed. What counts as timely is entirely the proxy's setting, so the same slow request can succeed behind one proxy and fail behind another. Vendors also split the case further: Cloudflare uses its own non-standard 524 when it connected to the origin but the reply took too long. And if the browser or client gives up before the proxy does, nobody receives a status code at all; nginx records that as 499 in its own log.

Seen in a real outage

Timeouts often have nothing to do with a slow query. At incident.io, requests waited up to 20 seconds just to borrow a database connection from the app's own pool, while the database itself looked idle.

Your turn

Which code is it?

A CSV export fails for large accounts only. Every failure takes exactly 60.0 seconds and shows an nginx error page. The app's log shows the same export finishing at 74 seconds with status 200. Which code did the user get?

Often confused with

504 questions

Who sends a 504 Gateway Timeout?
Typically a reverse proxy, load balancer or CDN. It forwarded the request, waited for its configured timeout, and answered 504 itself because the server behind it had not replied yet.
Does a 504 mean my request failed?
Not necessarily. It means the proxy stopped waiting. The application may still complete the work afterwards, which is why retrying a write without an idempotency key can apply it twice.
How do I fix a 504 on nginx?
Find out what is slow before touching the timeout: check the database for long queries, the app for slow outbound calls and for exhausted workers or connections. Raise proxy_read_timeout only for jobs that are meant to be long, or move them to a background job.

Sources: RFC 9110: 504 Gateway Timeout (section 15.6.5) ↗ · nginx: proxy_read_timeout ↗ · AWS: Troubleshoot your Application Load Balancer ↗ · Report a problem

Diagrams are our own simulations. All wording is original and paraphrases the linked sources.

Practise this in the DNS / HTTP track

Two-minute animated lessons, then boss fights from real outages. Free, no account needed.

Start the track