HTTP 504 Gateway Timeout is sent by a proxy, CDN or load balancer when the server behind it did not respond within the time limit. It is defined in RFC 9110, section 15.6.5. Unlike 502, where the upstream answered badly or not at all, here the upstream is probably alive but too slow, or stuck.
The limits that produce a 504
| Layer | Typical default | Setting |
|---|---|---|
| nginx | 60 s | proxy_read_timeout (and proxy_connect_timeout, proxy_send_timeout) |
| AWS load balancer | 60 s idle | idle timeout |
| Cloudflare | 100 s | not changeable on most plans; it answers 524 |
| Application server | varies | for example the worker timeout |
| Your own HTTP client | varies | connect and read timeouts |
If the failure occurs after a round number of seconds, you have found the layer: the time matches one of these limits. Check each hop; the shortest timeout wins, and it is easy to raise one limit and forget the one in front of it.
Why the upstream is slow
- A slow query or a missing index: the database does work that grows with the data.
- A call to another service that has no timeout of its own and hangs.
- Heavy work in the request: a report, an export, image processing, sending e-mail.
- A queue of requests: all workers are busy, and new requests wait. Look at the worker count and the queue length.
- A deadlock or an endless loop in the application.
- Cold starts of serverless functions or of a container that scaled from zero.
Find the slow step
- Measure the same request directly against the application, not through the proxy:
curl -w '%{time_connect} %{time_starttransfer} %{time_total}\n' -o /dev/null -s http://127.0.0.1:3000/report. The curl converter helps if you want the same measurement in code. - Add timing to the log: the proxy’s
$upstream_response_timein nginx, and timings per step inside the application. - Profile the slow path: the database’s slow query log, a trace of the downstream calls.
- Compare fast and slow requests: the difference in input usually shows the cause.
Fix the cause, not the number
- Make the work faster: an index, a cache, a smaller response.
- Put a timeout on every outgoing call inside the application, shorter than the proxy’s, so that the application can fail with a useful message.
- Move long work out of the request. Answer
202 Acceptedwith a link to a status resource, run the job in the background and let the client poll or receive a webhook. - Only then, and only for a path that really needs it, raise the proxy’s timeout for that location.
The HTTP status codes tool explains 504 next to 408 and the Cloudflare timeout codes 522 and 524, which are easy to confuse. If you are not sure whether the failure is slowness or a crash, compare with 502 and 503.
Frequently asked questions
Should I just increase the timeout?
Only as the last step. A request that takes a minute is already a poor design for a web call, and a higher limit lets slow requests pile up and use all the workers. First find out why it is slow, or move the work into a background job.
What is the difference between 504 and 408?
408 Request Timeout means the server waited too long for the client to send its request. 504 is about the other direction: a proxy waited too long for the server behind it.
Why is the 504 after exactly 60 seconds?
A round number points at a configured limit, not at a random failure. Common defaults are 60 seconds for nginx (proxy_read_timeout) and for the idle timeout of an AWS load balancer, and 100 seconds for Cloudflare. Match the number to the layer.
Can retrying cause harm after a 504?
Yes for non-idempotent requests. The proxy gave up, but the application may still be working and may finish the job. Retry a POST only with an idempotency key, or check the result first.
More in this reference
- HTTP 301 Moved Permanently: redirects, caching and SEO
- HTTP 302 Found: temporary redirects, and when to use 303 or 307 instead
- HTTP 304 Not Modified: how conditional requests and ETags save bandwidth
- HTTP 400 Bad Request: the common causes and how to find the bad part
- HTTP 401 Unauthorized: missing or invalid credentials, and 401 vs 403
- HTTP 403 Forbidden: why access is refused and how to fix it
- HTTP 404 Not Found: causes, how to debug it and what to return instead
- HTTP 429 Too Many Requests: what it means and how to handle it
- HTTP 500 Internal Server Error: how to find the cause and what to tell users
- HTTP 502 Bad Gateway: finding the broken link between the proxy and the app
- HTTP 503 Service Unavailable: maintenance pages, overload and Retry-After