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. Here's 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.
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.
Related 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