A 504 Gateway Timeout is a waiting problem. Somewhere between your visitor and your website's code, one server asked another for a response and gave up after waiting too long. The page never arrives, and the visitor sees an error instead.
The tempting fix is to raise the timeout and move on. Sometimes that's appropriate. More often, though, a 504 is a symptom of something slow that deserves attention: a heavy database query, an overloaded server, an external service that's lagging, or a network problem between a proxy and your origin. This guide shows how to figure out which.
What a 504 Gateway Timeout means
Most websites have at least two layers. A front server, such as a CDN, load balancer or Nginx acting as a reverse proxy, receives the visitor's request and forwards it to a back end, like PHP-FPM or an application server. The front server waits a set amount of time for the answer.
If that time runs out before the back end responds, the front server returns a 504. Compare that with its close relative:
- 502 Bad Gateway: the back end sent an invalid response or refused the connection.
- 504 Gateway Timeout: the back end didn't answer in time.
You might see "504 Gateway Timeout," "Gateway Time-out," "HTTP Error 504," "504 Gateway Timeout nginx," or a CDN-branded page saying the origin server timed out.
Quick checks for visitors
- Wait a minute and reload. Timeouts during busy periods often clear quickly.
- Try another browser, device or network to make sure the issue isn't local.
- If you were submitting a form or payment, don't keep clicking submit. Check your email or account first, since the action may have gone through even though the page timed out.
If the site stays unreachable for everyone, the cause is on the server side.
Common causes of 504 errors
Slow scripts and long-running requests
Pages that do a lot of work, like large reports, imports, exports, search across big datasets, or complex WooCommerce pages, may simply take longer than the proxy is willing to wait.
Slow database queries
A missing index, a bloated table or a plugin running an inefficient query can make every page wait on the database. When the database is slow, everything above it times out.
An overloaded server
If the CPU is maxed out, memory is exhausted, or all PHP workers are busy, requests queue up. The ones at the back of the queue eventually time out. Traffic spikes and bots are frequent triggers.
Slow external services
Many sites call outside APIs while building a page, such as payment providers, shipping rate calculators, social feeds, license checks or font and analytics services loaded server-side. If one of them is slow or down, your page waits for it.
Network or firewall problems between proxy and origin
If you use a CDN, it must reach your server. A firewall that silently drops the CDN's traffic, a changed server IP that DNS still points to, or routing trouble can cause the CDN to wait until it times out.
Timeout settings that don't match
Each layer has its own timeout. If Nginx waits 60 seconds but PHP is allowed to run for 300, or the CDN gives up before your server does, you'll get 504s on tasks that would otherwise finish.
How to fix a 504 Gateway Timeout
1. Find out when and where it happens
Start by narrowing the problem down:
- Does it happen on every page or only certain ones?
- Is it constant or only at busy times?
- Does it affect logged-in users, the admin area or checkout specifically?
A single slow page points to code or queries. Site-wide timeouts at peak times point to capacity. Constant site-wide timeouts point to a stopped service or a network problem.
2. Read the logs
On a VPS, the Nginx error log (/var/log/nginx/error.log) will show lines like "upstream timed out (110: Connection timed out) while reading response header from upstream." That confirms the back end was too slow and tells you which URL triggered it.
Then check the back end. PHP-FPM can log slow requests if you enable its slow log by setting request_slowlog_timeout and slowlog in the pool configuration. The slow log records a stack trace showing exactly which function was running when the request dragged on, which is enormously helpful.
On shared hosting, check the error log in your control panel and your resource usage graphs.
3. Check server load
Use top or htop to look at CPU and memory, and check whether PHP workers are all busy. If you're constantly at the limit, you have three options: reduce the work (caching, fixing heavy plugins), block wasteful traffic (bots), or add resources.
4. Speed up the database
- Enable the MySQL or MariaDB slow query log to find the worst offenders.
- Add indexes to columns used in frequent lookups if you control the code.
- Clean up bloated tables. In WordPress, autoloaded options, expired transients, post revisions and old logs from plugins are common sources of bloat.
- Consider an object cache such as Redis for busy dynamic sites.
5. Deal with slow external calls
If a page waits on an outside API, add sensible timeouts in your code so it fails gracefully instead of hanging, cache API responses where you can, and move non-urgent calls to background jobs. In WordPress, temporarily disabling plugins that connect to outside services is a quick way to test this.
6. Move heavy tasks to the background
Imports, exports, backups, bulk email and image processing shouldn't run inside a normal web request. Run them through a cron job, a queue worker or the command line, where web timeouts don't apply.
7. Adjust timeouts, carefully
If a legitimate task needs more time, align the timeouts across layers. On Nginx with PHP-FPM, the relevant settings include fastcgi_read_timeout (or proxy_read_timeout for proxied apps) in Nginx, and max_execution_time in PHP, plus request_terminate_timeout in the FPM pool if it's set. For example, in an Nginx location block:
fastcgi_read_timeout 120s;
Then test and reload with sudo nginx -t and sudo systemctl reload nginx.
Keep in mind that CDNs usually have their own maximum wait time that you may not be able to raise on lower-tier plans. For anything that genuinely takes minutes, background processing is the better fix.
8. Check the CDN-to-origin connection
If your CDN shows the 504, test the origin directly by temporarily bypassing the CDN. If the origin is fast, check that your firewall allows the CDN's IP ranges and that DNS at the CDN points to the right server IP. Our guide to DNS records is a handy refresher if you need to verify that A record.
Preventing 504 errors
- Use page caching so most visitors never hit PHP or the database.
- Keep plugins lean and remove ones you don't need.
- Monitor uptime and response times so slowdowns are caught early.
- Schedule heavy jobs outside peak hours.
- Make sure your plan has enough CPU and memory for your real traffic. If you're running into limits often, a VPS or a larger plan from a host like Host Whimsy gives your site more breathing room.
The bottom line
A 504 Gateway Timeout means a server waited too long for an answer. Rather than just raising the timeout, find out what's slow: check the logs, look at server load, examine database queries and external API calls, and move heavy work into the background. Fix the slowness and the timeouts usually disappear along with it.