HTTP 504 Error: Gateway Timeout

When your proxy waits for a response that never comes

The HTTP 504 Gateway Timeout error occurs when a server acting as a gateway or proxy doesn't receive a response from the upstream server within the allotted time. It's a 5xx family error indicating a server-side problem, specifically in the communication chain between different infrastructure components.

Unlike the 502 Bad Gateway where the backend responds but invalidly, the 504 indicates a complete absence of response within the allocated time. The proxy successfully transmitted the request to the backend, but the backend didn't respond before the timeout was reached. The backend's silence is the problem.

In modern architectures with reverse proxies, CDNs and load balancers, the 504 is a common error that can have various causes: overloaded backend, requests taking too long, network issues, or simply misconfigured timeouts. MoniTao monitors your endpoints and alerts you immediately when 504 errors occur.

Common causes of 504 errors

The 504 always indicates a response time problem. Here are the most common causes to investigate:

  • Overloaded backend: the application server is under heavy load (100% CPU, saturated memory, too many simultaneous requests). It cannot process requests quickly enough and some exceed the timeout.
  • Proxy timeout too short: the timeout configured on the proxy (Nginx, HAProxy, Cloudflare) is less than the normal processing time of some requests. This often happens after adding slower features.
  • Slow database queries: unoptimized SQL queries, unindexed tables, or deadlocks can block the application for many seconds, causing timeouts.
  • Network issue: the connection between proxy and backend is unstable, with packet loss or excessive latency. The backend may be responding, but the response doesn't arrive in time.

Where 504 occurs in your architecture

Identifying the component generating the 504 is the first diagnostic step:

  • Nginx → PHP-FPM: classic LEMP configuration. The 504 occurs when the PHP script exceeds Nginx's fastcgi_read_timeout or PHP-FPM's request_terminate_timeout.
  • Cloudflare → Origin: Cloudflare enforces strict timeouts (100s free plan, up to 6000s Enterprise). If your origin server is slower, Cloudflare generates a 504.
  • Load Balancer → Backend: a backend in the pool is no longer responding or is too slow. The load balancer times out and may retry on another backend if configured.
  • API Gateway → Microservice: in a microservices architecture, a downstream service may be slow or blocked, propagating 504s in cascade through the chain.

Solutions to fix 504 errors

Resolution depends on the identified cause. Here are the most effective approaches:

  • Increase timeouts: if long requests are legitimate, increase proxy timeouts. Warning: this doesn't solve the root problem and can mask performance issues.
  • Optimize backend: analyze slow queries (MySQL slow query log, APM), add indexes, optimize application code. This is the sustainable solution.
  • Implement caching: for repetitive and expensive requests, add a cache layer (Redis, Memcached, Varnish) to reduce backend load.
  • Switch to async: for truly long operations (report generation, exports), use an asynchronous job system with notification when ready.

Timeout configuration to avoid 504

Here are recommended configurations for different components of your stack:

# Nginx - /etc/nginx/nginx.conf
location ~ \.php$ {
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    fastcgi_read_timeout 300s;     # 5 minutes max
    fastcgi_send_timeout 300s;
    fastcgi_connect_timeout 60s;
}

# For a proxy to HTTP backend
location /api/ {
    proxy_pass http://backend;
    proxy_read_timeout 300s;
    proxy_connect_timeout 60s;
    proxy_send_timeout 300s;
}

# PHP-FPM - /etc/php/8.2/fpm/pool.d/www.conf
request_terminate_timeout = 300

# MySQL - Slow query log to identify slow queries
slow_query_log = 1
long_query_time = 2

These configurations set a 5-minute timeout, sufficient for most operations. Adjust according to your specific needs. MySQL slow query log helps identify queries to optimize.

Monitoring 504 errors with MoniTao

MoniTao helps you detect and quickly resolve 504 errors:

  • Real-time detection: as soon as an endpoint returns a 504, MoniTao captures it and enters the verification phase.
  • Response time measurement: each check records response time. You can identify endpoints approaching timeout before they break.
  • Configurable alerts: define alert thresholds not only on response code but also on response time (alert if > 5s for example).
  • Performance history: analyze response time evolution over time to detect progressive degradations before they cause 504s.

Quick resolution checklist

  • Identify which component generates the 504 (proxy, CDN, load balancer)
  • Check backend load: top, htop, vmstat
  • Review database slow query logs
  • Compare configured timeouts with actual response times
  • Test backend directly (without going through proxy)
  • Monitor after fix to confirm resolution

Frequently asked questions about 504 errors

What's the difference between a 502 and a 504 error?

The 502 Bad Gateway indicates the backend responded but with an invalid or malformed response. The 504 Gateway Timeout indicates the backend didn't respond at all within the allotted time. The 502 is a response content problem, the 504 is a response time problem.

How to increase timeout on Cloudflare?

Cloudflare timeout depends on your plan: 100 seconds (Free/Pro), 600 seconds (Business), up to 6000 seconds (Enterprise). For lower plans, you must optimize your backend or use Cloudflare Workers for long operations.

What timeout should I configure for my application?

A 30-second timeout is sufficient for 99% of normal web requests. Beyond that, it's usually a sign of performance problems. For legitimately long operations (exports, reports), provide dedicated endpoints with higher timeouts.

Is the 504 error bad for SEO?

Yes. Google interprets repeated 504s as a sign of unavailability. If Googlebot regularly encounters 504s, it can negatively affect crawling and ranking of your pages.

How to know if it's the proxy or backend timing out?

Test the backend directly without going through the proxy (curl on local IP or socket). If the backend responds correctly directly but not through the proxy, it's a proxy configuration issue. If the backend is slow even directly, it's an application performance problem.

Can automatic retry be configured on 504?

Yes, but with caution. Nginx allows configuring proxy_next_upstream to retry on another backend. However, retrying POST requests can cause duplicates. Reserve automatic retry for idempotent requests (GET, HEAD).

Conclusion

The HTTP 504 Gateway Timeout error signals a response time problem in your infrastructure. The cause is usually an overloaded backend, slow requests, or misconfigured timeouts. Diagnosis involves identifying the faulty component and analyzing response times.

The sustainable solution is to optimize backend performance rather than simply increasing timeouts. MoniTao helps by monitoring not only response codes but also response times, alerting you before performance issues become 504 errors.

Ready to Sleep Soundly?

Start free, no credit card required.