504 Gateway Timeout
A gateway or proxy waited too long for a response from the upstream server and gave up before the page was ready.
What 504 Gateway Timeout means
The proxy (nginx, a load balancer, a CDN) reached the upstream but did not get a full response within its timeout. The upstream may still be working on the request.
502 means the upstream gave a bad or broken response. 504 means no response in time. 408 is the reverse case, where the server gave up waiting for the client.
Common causes
- A slow database query, report or third-party API call holding up the request.
- A proxy timeout shorter than the app needs, such as nginx's proxy_read_timeout (60 seconds by default) or a CDN's own limit.
- An overloaded app server with requests queuing up.
- Network problems between the proxy and the origin.
How to fix 504 Gateway Timeout
- As a visitor, wait and retry. If you submitted a form or payment, check whether it went through before sending it again.
- Find the slow step with app logs or tracing and speed it up, for example with a database index or a cache.
- Move long work to a background job and return 202 with a status URL.
- If the work really needs more time, raise proxy_read_timeout in nginx and the matching limit on any CDN or load balancer.
Example response
HTTP/1.1 504 Gateway Timeout
Content-Type: text/plain; charset=utf-8
504 Gateway TimeoutHow to check for a 504 response
cURL
# Print only the status code
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/JavaScript (Node.js)
const res = await fetch("https://example.com/");
if (res.status === 504) {
console.log(res.status, res.statusText);
}Python
import requests
res = requests.get("https://example.com/")
if res.status_code == 504:
print(res.status_code, res.reason)Go
res, err := http.Get("https://example.com/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode == 504 {
fmt.Println(res.Status)
}Specification
504 Gateway Timeout is defined in RFC 9110, section 15.6.5.
Related status codes
- 502
A server acting as a gateway or proxy got an invalid response, or no usable response, from the upstream server behind it.
- 503
The server cannot handle the request right now, usually because it is overloaded or down for maintenance. Try again later.
- 408
The server closed the connection because the client took too long to send the full request.