Performance

How to Fix 504 Gateway Timeout Error on Your Site

Facing a 504 gateway timeout error? Discover why your server failed to respond and follow our tactical steps to diagnose, fix, and restore uptime immediately.

Daniel ReissBy Daniel Reiss·October 27, 2026·How we test

What Causes a 504 Gateway Timeout Error?

A 504 Gateway Timeout is an HTTP status code indicating that an intermediary server, such as a reverse proxy, web application firewall, or load balancer, did not receive a timely response from an upstream server while attempting to complete a web request. In practical web hosting terms, an edge gateway like Cloudflare or an internal Nginx proxy waited for your backend PHP-FPM worker or database to process data, but the origin took too long and the gateway severed the connection before receiving any HTML output.

While local network glitches and misconfigured client proxies can occasionally produce this error, the overwhelming majority of 504 incidents stem directly from backend resource saturation, sluggish database queries, or misconfigured reverse proxy timeout rules. Resource-constrained shared hosting plans frequently trigger these timeouts during unexpected traffic surges, because concurrent backend processes deplete allocated CPU and RAM limits, leaving incoming connection queues stalled until the gateway drops them.

First-Line Diagnostic Steps: Isolate the Gateway

Before modifying core configuration files or restarting backend daemons, establish whether the timeout originates at your CDN edge or directly on your web host. If you employ Cloudflare, Sucuri, or an AWS CloudFront distribution, inspect the error page carefully; a branded Cloudflare 504 explicitly confirms that the CDN reached your origin IP address, but your server failed to reply within Cloudflare's strict 100-second window, ruling out DNS and edge-network failures.

You can definitively verify origin server health by bypassing your reverse proxy entirely using a local hosts file modification. By routing your domain name directly to the origin server's public IP address on your workstation, you test the backend in isolation; if the page renders without issue, your edge timeout or caching rules require adjustment, whereas a persistent timeout confirms that the underlying application code or database is freezing.

Tuning Backend Timeouts and Server Resources

When your front-facing web server delegates dynamic requests to a FastCGI process manager like PHP-FPM, conservative execution ceilings will trigger premature 504 disconnects during heavy operations like database migrations or bulk imports. Inside your Nginx site configuration, lengthen the proxy directives fastcgi_read_timeout and proxy_read_timeout from the default 60 seconds to 300 seconds, and align these numbers with the max_execution_time directive inside your php.ini configuration.

Lengthening timeout thresholds helps legitimate long-running scripts complete, but inflating these limits without monitoring system load will quickly exhaust server memory under heavy traffic. Inspect your PHP-FPM process pool logs located in /var/log/php-fpm/ to confirm whether your workers are maxing out; if your pool regularly hits pm.max_children limits, pending connections will back up in the listen queue until the front-end proxy server times out and serves an HTTP 504 response.

Common Misconfigurations and Future Prevention

A frequent mistake webmasters make is blaming their CDN provider for a 504 error rather than diagnosing unindexed database queries or stalled third-party API calls. Developers often mask the underlying architectural flaw by continually increasing server execution limits, which merely prolongs site hangs during synchronous calls to external services like payment gateways, external font providers, or unresponsive transactional mailers.

To permanently prevent future gateway timeouts, offload resource-heavy operations to asynchronous background queues using Redis or RabbitMQ instead of processing them during an active HTTP request cycle. Additionally, implementing an in-memory object cache and transitioning demanding web applications from congested shared hosting environments to a scalable VPS with dedicated CPU cores will ensure that traffic spikes never leave your origin server unresponsive.

Frequently asked

Questions readers ask about this topic

What is the difference between a 502 Bad Gateway and a 504 Gateway Timeout?

A 502 Bad Gateway means the edge proxy received an invalid or corrupted response from the upstream server, whereas a 504 Gateway Timeout indicates the proxy received no response at all before the connection timer elapsed.

Can a site visitor fix a 504 gateway timeout error?

Generally no, because 504 errors stem from server-side infrastructure bottlenecks or application execution limits. However, visitors can rule out local network issues by clearing browser cache, disabling active VPN connections, or flushing their local DNS.

Why does Cloudflare show a 504 error on my website?

Cloudflare enforces a strict, non-negotiable 100-second timeout on standard plans. If your origin web server takes longer than 100 seconds to generate a response for dynamic requests, Cloudflare automatically drops the socket and displays a branded 504 page.

Will increasing PHP max_execution_time fix my 504 error?

It can resolve timeouts for resource-intensive administrative tasks, but only if you also raise the corresponding timeout settings in Nginx or Apache. If the web server proxy timeout is lower than PHP's execution time, the server will still disconnect early.
Keep exploring · Performance

Where to go next on Hostilo

Newsletter

One email a month. Hosting deals, new reviews, no fluff.

Related reading