504 Gateway Timeout
A proxy or load balancer passed your request to the server behind it and gave up waiting for the answer. The upstream may still be working on it.
What 504 means
Like 502, a 504 is sent by an intermediary, never by the application that is slow. The proxy reached the upstream, or at least tried to, and its timer ran out before a response came back.
One consequence is easy to miss: the proxy stopped waiting, but the upstream was not necessarily told to stop working. A report that timed out may finish a minute later, and a payment that returned 504 may have gone through. Check before you repeat anything that changes data.
Which timeout fired
The time until the error appears is the best clue. A 504 that always arrives after exactly 30 or 60 seconds points at a configured limit, and the layer with that limit is the one that sent it.
| Proxy | Setting | Default |
|---|---|---|
| nginx (HTTP upstream) | proxy_read_timeout | 60s between two reads, not for the whole response |
| nginx (connecting) | proxy_connect_timeout | 60s; cannot usually exceed 75s |
| nginx (PHP-FPM and other FastCGI) | fastcgi_read_timeout | 60s |
Apache mod_proxy | ProxyTimeout, or timeout= on ProxyPass | The value of Timeout (60 seconds) |
nginx logs the event as upstream timed out (110: Connection timed out) while reading response header from upstream. If the line ends in while connecting to upstream, the upstream never accepted the connection: think firewall, wrong address or a full accept queue rather than slow code.
Cloud load balancers and CDNs have idle timeouts of their own. Behind Cloudflare, an origin that is too slow is reported as 524, not 504.
Why the upstream was slow
- A database query that scans a large table, or one waiting on a lock held by another request.
- A call to a third-party API with no timeout of its own, so your request waits as long as theirs does.
- Work that is simply long: exports, report generation, image or video processing, large imports.
- Every worker busy. The request sat in a queue and the proxy's timer expired before a worker picked it up.
- A cold start: the first request after a deploy or a scale-up waits for the application to boot.
Fixing it
- Find the slow part first. Add
$request_timeand$upstream_response_timeto the nginxlog_formatand sort by them; enable the database's slow query log. - Make the request shorter. Add the missing index, cache the result, paginate.
- Move long work out of the request. Accept the job, return at once with a job ID, and let the client poll or receive a webhook when it is done.
- Set timeouts on outbound calls that are shorter than the proxy's, so your application can return a useful error instead of being cut off.
- Raise the proxy timeout last, and in every layer on the path, since the shortest one wins. A higher limit keeps slow requests alive for longer and ties up workers, so it suits a known long endpoint better than the whole site.
As a client
Retry reads after a pause. For writes, find out whether the first attempt succeeded before sending it again, or use an idempotency key if the API offers one. If the site is not yours, all you can do is try later or check whether it is slow for everyone.
504 compared with 502, 408 and 503
| Code | Who waited for whom | Outcome |
|---|---|---|
| 504 Gateway Timeout | A proxy waited for the upstream's response | Proxy gave up |
| 502 Bad Gateway | No waiting: the upstream refused, dropped or garbled the reply | Proxy had nothing valid to forward |
| 408 Request Timeout | The server waited for the client's request | Server gave up |
| 503 Service Unavailable | Nobody waited | The request was turned away at once |
The mirror image, where the client gives up before the upstream answers, is logged by nginx as 499.
Example 504 response
HTTP/1.1 504 Gateway Timeout
Server: nginx
Content-Type: text/html
Content-Length: 152
Connection: keep-alive
<html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx</center>
</body>
</html>nginx spells the reason phrase "Gateway Time-out" in its stock page; the registered name is "Gateway Timeout". Clients should only ever act on the number.
Defined in RFC 9110, section 15.6.5.
Questions
Is a 504 Gateway Timeout my fault?
No. It means a server on the site's side took too long to answer its own proxy. Your connection and browser are not involved, so the only thing to do as a visitor is try again later.
How do I increase the timeout in nginx to stop 504 errors?
Set proxy_read_timeout (or fastcgi_read_timeout for PHP-FPM) in the location that proxies the slow endpoint, for example proxy_read_timeout 300s. The default is 60 seconds. Any load balancer or CDN in front of nginx has its own limit, which must be raised as well.
Did my request go through if I got a 504?
It may have. The proxy stopped waiting, but the server behind it can carry on and finish the work. For anything that changes data, check the result before retrying.
What is the difference between 504 and 408?
They time out in opposite directions. A 408 means the server did not receive the complete request from the client in time. A 504 means a proxy sent the request on and did not receive the response from the upstream server in time.