504 Gateway Timeout Error: What It Means, Why It Happens and How to Fix It
Quick answer: A 504 Gateway Timeout happens when a proxy or gateway (Nginx, a load balancer, Cloudflare) waits too long for the application server to respond. The usual causes are slow database queries, slow external API calls, an overloaded server or timeouts set too low. Fix what is slow, move heavy jobs to the background, and only then raise the timeouts in Nginx, PHP and the load balancer, keeping them consistent.
The 504 Gateway Timeout error means a server that sits between the visitor and your application gave up waiting. The request was sent correctly, but the server behind took too long to answer.
This guide explains what causes a 504, how to find the slow part, and how to fix it without simply hiding the problem behind bigger timeouts.
What does 504 Gateway Timeout mean?
A 504 is returned by a server acting as a gateway or proxy when it doesn't receive a response from the upstream server within its time limit:
Browser -> Cloudflare (100 s) -> Nginx (60 s) -> PHP-FPM / app
each layer has its own timeout; the shortest one wins
Unlike a 502, the connection worked; the answer just didn't arrive in time. If you are a visitor, try again later: the problem is on the site's side.
Common causes of a 504
- Slow database queries: missing indexes, huge tables, locks;
- Slow or unavailable external APIs: payment, shipping, CRM or other calls without a timeout;
- Heavy tasks in the request: reports, exports, imports, image processing or sending many emails;
- Overloaded server: CPU, memory or disk at the limit, or all workers busy;
- Timeouts that don't match: a proxy that gives up before the app finishes;
- Network problems: between the proxy and the backend, or slow DNS resolution of the upstream.
How to fix a 504 Gateway Timeout
1. Find out which layer timed out
Look at the proxy's error log:
sudo tail -n 50 /var/log/nginx/error.log
# upstream timed out (110: Connection timed out) while reading response header from upstream
The log line includes the URL of the request. If it is always the same few URLs, the problem is in those pages; if it is every URL, the server or the database is struggling. If you use Cloudflare and your server takes longer than Cloudflare's limit (100 seconds on most plans), Cloudflare shows its own error 524 instead.
2. Check server resources
Use top or htop, free -h and iostat to see whether CPU, memory or disk are saturated. Check whether PHP-FPM logged server reached pm.max_children: when all workers are busy, new requests wait in line and time out.
3. Find slow database queries
Turn on the MySQL/MariaDB slow query log to see which queries take more than a second:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SHOW VARIABLES LIKE 'slow_query_log_file';
Run EXPLAIN on the slow queries and add indexes to the columns used in WHERE, JOIN and ORDER BY. Check also for queries running inside loops (one query per row). See pagination and indexes in a PHP API.
4. Put timeouts on external calls
A request that waits for a slow third-party API will wait as long as that API takes. Always set a short timeout and handle the failure:
<?php
$ch = curl_init('https://api.example.com/rates');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 3, // seconds to connect
CURLOPT_TIMEOUT => 8, // seconds for the whole request
]);
$body = curl_exec($ch);
if ($body === false) {
error_log('Rates API failed: ' . curl_error($ch));
// use a cached value or show a friendly message
}
5. Move heavy work to the background
Reports, exports, bulk imports, video or image processing and mass emails don't belong in a web request. Save a job to a queue (a database table, Redis or a queue service), process it with a worker or cron job, and let the user check the status or receive the result by email.
6. Only then, adjust the timeouts
If a long operation is legitimate (an admin export that takes 2 minutes), raise the limits in every layer, so none of them gives up first:
# Nginx (PHP-FPM)
fastcgi_read_timeout 120s;
# Nginx (proxy to Node, Python, another server)
proxy_read_timeout 120s;
# PHP-FPM pool
request_terminate_timeout = 120
# php.ini
max_execution_time = 120
On Apache with mod_proxy, the equivalent is ProxyTimeout; on an AWS Application Load Balancer, the idle timeout (60 seconds by default). Test the configuration (sudo nginx -t or sudo apachectl configtest) before reloading. Raising timeouts for every page only makes visitors wait longer and ties up workers, so limit it to the routes that need it.
504 in APIs and cloud servers
In APIs, a 504 usually means the backend didn't answer within the gateway's limit. Review the maximum execution time, the server limits, the network latency between services and the time spent in each external call. For APIs called by mobile apps, keep endpoints fast and return a job ID for long operations instead of making the client wait.
504 vs. 502 and 503
| Code | Meaning |
|---|---|
| 502 Bad Gateway | The backend gave an invalid answer or refused the connection |
| 503 Service Unavailable | The server is overloaded or in maintenance |
| 504 Gateway Timeout | The backend took too long to answer |
If yours is a 502, see 502 Bad Gateway; for errors in the application itself, PHP 500 errors.
How to prevent 504 errors
- Monitor response times, not just uptime;
- Keep the slow query log on and review it regularly;
- Set timeouts on every external call;
- Use caching for expensive pages and queries;
- Run long jobs in the background;
- Keep timeouts consistent across CDN, load balancer, web server and PHP.
Conclusion
A 504 Gateway Timeout is a performance problem showing up as an error. The fix is to find what is slow (the database, an external API, a heavy task or an overloaded server) and make it faster or move it out of the request. Raising timeouts is the last step, not the first.
Frequently asked questions about 504 errors
Is a 504 my internet connection?
Almost never. The error comes from the site's servers. Try again later or from another network to confirm.
Should I just increase the timeout?
Only for operations that legitimately take long. Otherwise, a bigger timeout hides the problem and makes visitors wait longer.
Why does the 504 only happen sometimes?
Usually because of traffic peaks, scheduled jobs (backups, cron tasks) running at the same time, or a slow external service.
Does a 504 hurt SEO?
Occasional errors don't. Frequent 5xx errors make Googlebot crawl less and can eventually drop pages from the index.
What is Cloudflare's timeout?
About 100 seconds on most plans. If your server takes longer, Cloudflare shows error 524 (A timeout occurred).