What HTTP 504 means
The proxy reached the backend, but the backend took too long to answer. Usually a slow database query, a call to a slow third-party API, or a long-running job handled inside a request. 502 means the backend answered badly; 504 means it didn’t answer in time.
How to fix a 504 error
-
nginx gives up after
proxy_read_timeout(60 s by default). AWS ALB has a 60 s idle timeout; API Gateway has a 29 s integration timeout by default. - Raising timeouts hides the problem. Move long work to a background job and return 202.
- Find the slow part with the backend’s logs or tracing: database, external API or lock contention.
Retrying
Yes, usually. It is often temporary, so retry with exponential backoff and give up after a few attempts.
Example response
HTTP/1.1 504 Gateway Timeout
Date: Wed, 21 Oct 2026 10:30:00 GMT
Content-Type: application/json
How to return 504
res.status(504).json({ error: 'Gateway Timeout' });
from fastapi import HTTPException
raise HTTPException(status_code=504, detail="Gateway Timeout")
http.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout)
location /private/ {
return 504;
}
Related status codes
- 502 Bad Gateway : A proxy or gateway got an invalid response from the upstream server.
- 408 Request Timeout : The server closed the connection because the client took too long to send the request.
- 524 A Timeout Occurred : The origin accepted the connection but didn’t respond within 100 seconds.
Other 5xx status codes
500 Internal Server Error · 501 Not Implemented · 502 Bad Gateway · 503 Service Unavailable · 505 HTTP Version Not Supported · 507 Insufficient Storage · 508 Loop Detected · 511 Network Authentication Required · 520 Web Server Returned an Unknown Error · 521 Web Server Is Down · 522 Connection Timed Out · 523 Origin Is Unreachable · 524 A Timeout Occurred · 525 SSL Handshake Failed · 526 Invalid SSL Certificate
See the full list of HTTP status codes.
Sources
Spotted a mistake? Email hello@devtoolsmith.com and we’ll fix it.
Frequently asked questions
What does HTTP 504 Gateway Timeout mean?
HTTP 504 Gateway Timeout means a proxy or gateway didn’t get a response from the upstream server in time. The proxy reached the backend, but the backend took too long to answer. Usually a slow database query, a call to a slow third-party API, or a long-running job handled inside a request. 502 means the backend answered badly; 504 means it didn’t answer in time.
How do I fix HTTP 504?
nginx gives up after proxy_read_timeout (60 s by default). AWS ALB has a 60 s idle timeout; API Gateway has a 29 s integration timeout by default. Raising timeouts hides the problem. Move long work to a background job and return 202. Find the slow part with the backend’s logs or tracing: database, external API or lock contention.