Fix "504 Gateway Timeout" on Your Website
504 Gateway Timeoutupstream timed outrequest took too longA 504 means nginx waited for the backend and it didn't answer in time. Unlike a 502 (bad response), a 504 is purely about slowness — a script, a query, or an overloaded backend. Here is how to fix it.
Cause 1: a slow script or query
The usual cause — a long-running PHP script or a slow database query exceeds the proxy timeout. Find it in the slow log and optimise it (add an index, cache the result, or move heavy work to a background job).
Cause 2: the backend is overloaded
If PHP-FPM has too few workers for the traffic, requests queue and time out. Raise pm.max_children sensibly (watch RAM) so requests aren't waiting in line.
Cause 3: timeout set too low
If the work is legitimately long (an import, a report), raise the timeout — but treat that as a last resort, not a fix for slow code:
# nginx
fastcgi_read_timeout 120s;
proxy_read_timeout 120s;
A 504 is a symptom of something slow. Bumping the timeout stops the error but the request is still slow for users. Optimise the script/query first.
504 = too slow. Find the slow script/query and fix it; add FPM workers; only raise timeouts as a last resort.
Cause 4: an external API or remote request hanging
If your code calls an external API (payment gateway, webhook, another service) with no timeout set, a hung remote call blocks the whole request until nginx gives up. Always set a short, explicit timeout on outbound HTTP calls in your code.
Cause 5: DNS or network latency to a dependency
A slow DNS lookup or a network hiccup to your database or cache server can silently add seconds per request. If 504s are intermittent rather than constant, check the network path and latency to any external dependency.
How to prevent 504s
- Add database indexes and cache expensive queries.
- Set explicit timeouts on any outbound API/HTTP call.
- Monitor slow-query and PHP-FPM slow logs proactively, not just after an outage.
- Size pm.max_children to your traffic and RAM.
Related errors
A 504 (too slow) is different from a 502 Bad Gateway (a bad/no response, often a crash). If it's specifically MySQL that's slow or refusing connections, see MySQL too many connections. For a WordPress site that's generally slow, see how to speed up WordPress.
Fast NVMe web hosting
Our web hosting runs on fast NVMe and tuned PHP-FPM, so slow timeouts are rare — DDoS protection included.
Frequently asked questions
What does 504 Gateway Timeout mean?
Nginx forwarded the request to the backend but didn't get a response within the timeout. The backend (a PHP script, a query, an app) is too slow or overloaded.
How do I fix a 504 error?
Find and optimise the slow script or database query, add more PHP-FPM workers if requests are queuing, and only raise the proxy/FPM timeout as a last resort for genuinely long tasks.
Is 504 the same as 502?
No. 504 means the backend was too slow (timed out); 502 means it returned a bad or empty response (often crashed). Different causes, different fixes.
Can a slow external API cause a 504 on my own site?
Yes — if your code waits on a payment gateway, webhook or third-party API with no timeout, a hung remote call blocks the whole request until nginx's own timeout is reached. Set short timeouts on every outbound call.
Is it safe to just keep raising the timeout value?
As a last resort for genuinely long tasks (large imports, reports), yes — but treat it as a stopgap. If typical page loads need a raised timeout, something is slow and users are feeling it even without an error.
Why are my 504s intermittent instead of constant?
That points to load-dependent slowness — a query that's fine at low traffic but slow under load, or a backend running out of PHP-FPM workers during peaks. Check pm.max_children and slow logs during the busy periods.
Go deeper on this
Guides and explainers that pair with this fix — from our guides and blog.
How to Speed Up a WordPress Site
Caching, images, PHP version and the hosting underneath.
ReadHow to Secure a WordPress Site
Updates, passwords, plugins and backups that actually matter.
ReadHow to Choose a Web Host
What to look for beyond the headline price.
ReadcPanel vs DirectAdmin vs Plesk
Which control panel does what, and what it costs.
ReadRelated articles
Fix "500 Internal Server Error" on Your Website
A 500 error on your site? The usual causes — .htaccess, file permissions, PHP — and how to fix them.
Read fix Web HostingHow to Point a Domain to Your Server (DNS A Record)
How to point a domain at your server with an A record — and why it doesn't work instantly.
Read fix Web HostingFix Email Going to Spam (SPF, DKIM & DMARC Explained)
Mail landing in spam? Set SPF, DKIM and DMARC correctly so your email reaches the inbox.
Read fix