How to Fix the “504 Gateway Timeout” Error
A "504 Gateway Timeout" error means a server waited for a response from another server and gave up because it took too long. The key word is slow: something in the request chain did not finish in time. Fixing a 504 is really about finding what is slow and speeding it up.
Common causes
- A slow script or query that runs longer than the timeout allows.
- A heavy import, backup or cron task running in the foreground.
- An overloaded server that cannot respond quickly enough.
- A proxy or CDN (like Cloudflare) timing out while waiting for your origin.
Step 1: Identify the slow operation
Note what you were doing when the 504 appeared — importing a large database, running a report, loading a specific page. That points to the slow operation. Your error log and resource usage add detail.
Step 2: Speed things up
- Optimise the database so queries return faster — see optimising your database.
- Add caching to cut the work each request does — see LiteSpeed Cache.
- Break up big jobs — import large files in smaller chunks rather than all at once.
Step 3: For long, legitimate tasks
Some operations genuinely take a while. Where possible, run them via a cron job or over SSH instead of through the browser, so a web timeout does not interrupt them.
Frequently asked questions
504 vs 502 — what's the difference?
Both involve one server waiting on another. A 502 means it got a bad response; a 504 means it got no response in time. A 504 is specifically a timeout, so the fix centres on speed.
My database import keeps causing a 504.
Large imports through phpMyAdmin often time out. Split the file, or import over SSH with the command line, which is not bound by the web timeout. See importing a database.
It happens only behind Cloudflare.
Cloudflare has its own timeout waiting for your origin. If the origin is slow, speed it up as above; test with Cloudflare paused to confirm the origin itself responds in time.
Was this article helpful?