Skip to content
Web Hosting

Fix "504 Gateway Timeout" on Your Website

504 Gateway Timeoutupstream timed outrequest took too long
7 min read Updated 12 June 2026 ESAGAMES Team

A 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;
Raising the timeout hides the cause

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.

See web hosting
FAQ

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.

Further reading

Go deeper on this

Guides and explainers that pair with this fix — from our guides and blog.

Knowledge base

Related articles

Skip the troubleshooting

Managed, protected hosting in Frankfurt — we handle the Linux, the network and the DDoS so you don't have to.

Payments Secure checkout with cards, banking apps and digital wallets.

Choose the payment flow that fits your stack and region without leaving the platform.

Pay by Zen Visa Mastercard Paysafecard PaysafeCash Skrill Trustly Bancontact UnionPay iDeal WebMoney