Server timeout error screen showing 504 Gateway Timeout message
# website errors# troubleshooting

504 Gateway Timeout: What It Means and How to Fix It

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.

What Is a 504 Gateway Timeout?

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.

504 vs 502: What's the Difference?

These two are easy to confuse:

  • 502 Bad Gateway — the upstream server responded, but with something invalid or an error
  • 504 Gateway Timeout — the upstream server didn't respond at all within the allowed time

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.

What Causes a 504 Error?

Slow Database Queries

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.

Overloaded Application Server

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.

Third-Party Service Delays

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.

Network Issues Between Servers

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.

Misconfigured Timeout Values

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.

How to Diagnose a 504 Error

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.

How to Fix a 504 Gateway Timeout

Increase Proxy Timeout Settings

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.

Optimise Slow Database Queries

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.

Scale Your Server Resources

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.

Move Long Tasks to Background Jobs

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.

Check External Dependencies

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.

Preventing 504s in the Future

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.

Summary

A 504 Gateway Timeout means your proxy gave up waiting for an upstream server. The most likely culprits are:

  • Slow database queries — optimise or add indexes
  • Overloaded server — scale up or tune worker counts
  • Third-party API delays — add client-side timeouts
  • Timeout settings too low — increase proxy_read_timeout

Work through each one systematically and you'll track down the cause quickly.

More posts

What Is Generative AI? How It Works and What It Creates

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 more
What Is Cursor AI? The AI Code Editor Explained

Cursor 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 more
What Is Claude Opus? Anthropic's Most Powerful Model Explained

Claude 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 more

Subscribe to our PRO plan.

Looking to monitor your website and domains? Join our platform and start today.