504

HTTP 504 Gateway Timeout

5xx Server Error

5xx Server Error RFC 7231, Section 6.6.5

What is HTTP 504 Gateway Timeout?

The 504 (Gateway Timeout) status code indicates that the server, while acting as a gateway or proxy, did not receive a timely response from an upstream server. Unlike 502 (Bad Gateway) which indicates an invalid response, 504 specifically means the upstream server did not respond within the timeout period.

Common Use Cases

  • Slow upstream database queries
  • External API timeout behind proxy
  • Application server hung or deadlocked
  • CDN origin server timeout

Usage Example

When Nginx is configured with a 60-second timeout and the PHP-FPM process is stuck on a slow database query exceeding that limit, Nginx returns 504 Gateway Timeout. Identify and optimize slow queries, increase timeout values, or implement query caching to resolve this.

# Nginx - increase proxy timeout for slow endpoints
location /api/reports {
    proxy_read_timeout 120s;
    proxy_connect_timeout 30s;
    proxy_pass http://backend;
}

# Laravel - increase PHP execution time for long requests
set_time_limit(120);

Common Mistakes

⚠️

Mistake: Confusing 504 (timeout) with 502 (bad response)

Fix: 504 means the upstream server did not respond within the timeout period. 502 means it responded but with an invalid or empty response. Check upstream logs to distinguish between the two.

⚠️

Mistake: Simply increasing timeouts instead of fixing root causes

Fix: While increasing timeouts can resolve 504 errors temporarily, identify and fix the root cause — slow queries, external API latency, or resource contention. Infinite timeouts can mask serious performance issues.

Last updated: 21 Jun 2026