Find out why your server didn't answer the proxy in time

504 Gateway Timeout means a server acting as a gateway or proxy, such as a load balancer or the web server in front of your app, didn't get a response in time from the server behind it. A proxy only waits so long. A 504 means your server, or something your server was waiting on, took longer than that limit or never answered.

Why the server behind the proxy is too slow

  • A slow request

    A database query or report runs longer than the proxy waits. The proxy gives up and sends a 504, even if your app finishes the work afterward.

  • A slow service your app waits on

    Your app calls a payment provider or another outside API that hangs, so the app's own response waits with it.

  • Every worker is busy

    When all of the app's workers are tied up, new requests wait for a free one. Under load, that wait alone can pass the proxy's limit.

  • Traffic that never reaches the server

    A firewall or security rule that silently drops the proxy's traffic leaves the connection hanging until the proxy's connect timeout runs out.

  • A response shorter than it claims

    If your server sends a Content-Length larger than the body that follows, the proxy waits for bytes that never come.

RFC 9110 defines 504 as a gateway or proxy that didn't receive a timely response from the upstream server it needed. Amazon's load balancer guide lists a connection that can't be set up in time, a target that goes quiet past the idle timeout, blocked network traffic and a Content-Length larger than the body as causes of its 504s.

How the proxy's timeout works

  • What counts as in time is up to each proxy. nginx's proxy_read_timeout, for example, defaults to 60 seconds and counts the gap between two reads, not the whole response, while Amazon's Application Load Balancer gives up on setting up a connection to a server after 10 seconds.

  • MDN sets it apart from a 502: with a 504, the proxy got no HTTP response from the upstream within its time limit. If you run several proxies in a row, the one with the shortest limit is usually the one that sends the 504, so check each layer's timeout.

Measure where the time goes

# Time each stage of a request to the slow page
curl -sS -o /dev/null https://yoursite.com/slow-page \
  -w "connect %{time_connect}s, TLS %{time_appconnect}s, first byte %{time_starttransfer}s, status %{http_code}\n"

# From the proxy's machine, time the same request to the app directly (8080 here)
curl -sS -o /dev/null http://127.0.0.1:8080/slow-page \
  -w "first byte %{time_starttransfer}s, status %{http_code}\n"

Read the timings and the logs

  • If the 504 arrives just after a round number, such as 30 or 60 seconds, that number is probably a proxy's timeout. If the direct request to the app takes about as long, the app is the slow part; if it's fast, look at the network between the proxy and the app. time_starttransfer runs until the first byte arrives and includes the time the server spent working, as curl's manual explains.

  • In the proxy's error log, a timeout while connecting means the upstream never accepted the connection, so check firewalls and whether the server is up. A timeout while reading the response means it connected and then went quiet, so check your app's slow requests and its database for the same minute.

How to fix a 504 Gateway Timeout error

  1. Find the slow requestsLook in your app's logs or performance monitoring for requests that ran close to the proxy's limit, and note which pages and queries they were.
  2. Make the slow work fasterAdd the missing database index or cache an expensive result. Set a short timeout on calls to outside services so one slow API can't hold up the page.
  3. Move long jobs out of the requestRun a report or import as a background job and let the page check back for the result, instead of making the visitor's request wait for it.
  4. Raise a timeout only where you need itIf one endpoint has to run long, raise the proxy's limit for that path alone. A higher limit everywhere hides slow pages and keeps workers busy longer.
  5. Add capacity if every worker is busyIf requests wait for a free worker under load, add workers or servers, or find what keeps the existing ones busy.

Frequently asked questions

Why does the 504 appear after exactly 60 seconds?

Usually because a proxy in front of your site waits 60 seconds and your request takes longer. nginx's proxy_read_timeout and proxy_connect_timeout both default to 60 seconds, for example. Find the layer that times out, then make the request faster before you raise the limit.

What's the difference between a 504 and a 502?

Both come from a proxy. A 504 means the upstream didn't answer in time, and a 502 means it answered with something invalid or dropped the connection. The 502 Bad Gateway guide linked below covers the second case.

Can a 504 come from the visitor's side?

Sometimes. MDN notes that when the site works for other visitors, the visitor's own network setup, such as a VPN or proxy, can be the cause.