HTTP Error Codes

504 Gateway Timeout

HTTP 504

The gateway or proxy server didn't receive a response from the upstream server within the timeout period.

What It Means

A 504 error means your request made it to a proxy (like Nginx or a CDN), but the proxy gave up waiting for your application server to respond. The request took too long.

What happens:
User → Proxy → App Server (still processing...) → Proxy times out → 504

Common Causes

  • Slow database queries — Query takes longer than the proxy timeout
  • External API calls — Waiting on a third-party API that's slow or down
  • CPU-intensive operations — Heavy computation blocking the response
  • Network issues — Latency between proxy and app server
  • Insufficient resources — Server struggling under load
  • Proxy timeout too low — Timeout configured lower than typical request time

How to Debug

  1. Reproduce the slow request — Which endpoints are timing out?
  2. Profile the endpoint — What's taking so long?
  3. Check database queries — Are there slow queries? Missing indexes?
  4. Check external calls — Are third-party APIs responding slowly?
  5. Check server resources — CPU, memory, I/O

Nginx Timeout Configuration

# Increase timeouts in nginx.conf or site config
location / {
    proxy_pass http://app;

    # Time to establish connection
    proxy_connect_timeout 60s;

    # Time to send request
    proxy_send_timeout 60s;

    # Time to receive response (most relevant for 504)
    proxy_read_timeout 60s;
}

Finding Slow Queries

# PostgreSQL - find slow queries
SELECT query, calls, mean_time, total_time
FROM pg_stat_statements
ORDER BY mean_time DESC
LIMIT 10;

# MySQL - enable slow query log
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;

504 vs 408

  • 504 Gateway Timeout — Proxy gave up waiting for the app server
  • 408 Request Timeout — Server gave up waiting for the client to finish sending the request

Solutions

  • Optimize slow operations — Add indexes, cache results, optimize queries
  • Add timeouts to external calls — Don't let slow APIs block indefinitely
  • Move to background jobs — Long operations should be async
  • Increase proxy timeout — If the operation genuinely needs more time
  • Add caching — Reduce repeat expensive operations
  • Scale up/out — More resources to handle load

How to Monitor for 504 Errors

504 errors indicate performance degradation that's about to become downtime. UptimeSignal tracks response times and alerts you when endpoints start timing out. Catch slow responses before they become full timeouts. See also: 502 Bad Gateway, 522 Connection Timed Out.

Frequently Asked Questions

What causes a 504 Gateway Timeout?
A 504 occurs when a reverse proxy or load balancer doesn't receive a response within its configured timeout. Common causes: slow database queries, hanging external API calls, application deadlocks, resource exhaustion, or network issues between the proxy and backend server.
How do I fix a 504 Gateway Timeout?
Find the slow operation: check database slow query logs, application performance metrics, and external API response times. As a temporary fix, increase proxy timeouts (Nginx: proxy_read_timeout 120s). Long-term: optimize slow queries, add caching with Redis, and move long tasks to background workers.
What is the difference between 504 and 408?
504 Gateway Timeout is a proxy error -- the proxy timed out waiting for the backend. 408 Request Timeout means the server told the client their request took too long to send. 504 indicates backend slowness. 408 indicates client-side network issues. You'll see 504 from Nginx/CDN; 408 from the application server itself.
How do I increase Nginx timeout to prevent 504?
In your Nginx config: proxy_read_timeout 120s; proxy_connect_timeout 120s; proxy_send_timeout 120s;. But increasing timeouts is a band-aid -- the real fix is optimizing the slow backend operation. Monitor response time trends with UptimeSignal to catch degradation early.
Can Cloudflare cause 504 errors?
Cloudflare has a 100-second timeout on free/pro plans. If your origin takes longer than 100 seconds to respond, Cloudflare returns a 522 or 524 error (Cloudflare-specific versions of timeout). Enterprise plans allow custom timeout values. Optimize your backend to respond within 100 seconds.

Catch gateway timeouts before they cascade

UptimeSignal detects 504 timeout errors and alerts you within minutes.

Monitor response times and status codes. 25 monitors free, unlimited for $15/month.

Related Errors & Resources