
A 504 Gateway Timeout is one of those errors that tends to show up at the worst possible time — during a product launch, a sale, or when you've just pushed new code. It's frustrating, but it's also very fixable once you know what's going on.
A 504 error means a server acting as a gateway or proxy didn't receive a response from an upstream server in time.
Think of it like this: your web server asked your application server, database, or a third-party service for a response — and waited, and waited, and eventually gave up. The upstream server didn't crash (that would be a 502), it just took too long.
These two are easy to confuse:
Both point to a problem between your proxy/gateway and whatever is behind it. Check out our guide to common HTTP error codes for the full picture.
This is the most common cause on dynamic websites. A page that triggers a slow or unoptimised database query can take much longer than the proxy's timeout allows. When the query doesn't finish in time, the gateway gives up and returns a 504.
If your server is handling more requests than it can process, new requests queue up and wait. Eventually the proxy's timeout is hit before a worker becomes free.
If your application makes outbound requests to external APIs or services as part of handling a request, and one of those services is slow, your entire response time blows up. This is a common cause of 504s that's easy to overlook.
In multi-server setups, network latency or packet loss between your load balancer, web server, and application servers can cause timeouts even when each server individually is healthy.
If your proxy timeout is set too low (e.g. 10 seconds for a process that normally takes 15), you'll get intermittent 504s even when the server isn't particularly busy.
Check your error logs first:
# Nginx
sudo tail -f /var/log/nginx/error.log
# Apache
sudo tail -f /var/log/apache2/error.log
Look for "upstream timed out" messages — they'll usually tell you which upstream server is the problem and how long it waited.
Check server load:
top
vmstat 1
High CPU or I/O wait times during the timeout period point to a resource problem rather than a config problem.
Check for slow queries:
If you're running MySQL, enable the slow query log temporarily:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
This will log any query taking more than 2 seconds — often the smoking gun behind 504s.
In Nginx, adjust these values in your server block:
proxy_connect_timeout 120s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
fastcgi_read_timeout 120s;
This buys time for slower requests to complete. It's not a long-term fix on its own, but it stops the immediate 504s while you investigate.
Use EXPLAIN in MySQL or PostgreSQL to analyse slow queries. Add indexes where missing and consider caching frequently-read data with Redis or Memcached to reduce database load.
If the server is simply overloaded, the fix is more capacity — more PHP workers, more Gunicorn processes, or a bigger server. Review your essential server performance metrics to understand where the bottleneck is.
If certain operations (like generating reports, sending bulk emails, or processing files) are triggered by web requests and take a long time, move them to a background queue. The web request acknowledges immediately, the heavy work happens asynchronously.
Audit any outbound API calls your application makes during a request. If a third-party service is slow, consider adding a timeout to your own HTTP client calls so one slow dependency doesn't drag down your whole response time.
The best way to stay ahead of 504 errors is to monitor your site continuously. If a 504 starts occurring and you don't have monitoring in place, you might not find out for hours.
Setting up uptime monitoring means you'll know within a minute when your site starts returning errors, not when a customer reports it. You can also monitor response times over time — a gradual slowdown is often a warning sign of an impending 504.
Domain Monitor tracks uptime, response times, and alerts you the moment something looks wrong, so you can fix issues before they become incidents.
A 504 Gateway Timeout means your proxy gave up waiting for an upstream server. The most likely culprits are:
proxy_read_timeoutWork through each one systematically and you'll track down the cause quickly.
Generative AI creates new content — text, images, code, and more. This guide explains how it works, what tools are available, and where it's genuinely useful versus overhyped.
Read moreCursor AI is an AI-powered code editor built on VS Code. Learn what it does, how it works, and whether it's the right tool for your development workflow.
Read moreClaude Opus is Anthropic's most capable AI model, built for complex reasoning and demanding tasks. Learn what it does, how it compares, and when to use it.
Read moreLooking to monitor your website and domains? Join our platform and start today.