Error Codes Wiki

HTTP 504 Gateway Timeout — Backend Response Timeout Analysis

Critical5xx server error

Overview

Deep dive into HTTP 504 Gateway Timeout: diagnosing slow backends, proxy timeout tuning, and database query optimization to resolve upstream timeouts.

Key Details

  • 504 means the proxy waited too long for a response from the upstream backend
  • Different from 408 (client timeout) — 504 is between proxy and backend
  • Default Nginx proxy_read_timeout is 60 seconds
  • AWS ALB idle timeout default is 60 seconds, can be increased to 4000
  • Cloudflare has a fixed 100-second timeout on free plans (cannot be changed)

Common Causes

  • Backend processing a slow database query exceeding proxy timeout
  • Backend server overloaded with too many concurrent requests
  • Network latency between proxy and backend in different regions
  • Deadlocked backend threads unable to process new requests
  • Large file processing or report generation exceeding timeout
  • DNS timeout resolving the backend hostname

Steps

  1. 1Identify slow requests: check backend access logs for response times over 30s
  2. 2Increase proxy timeout: proxy_read_timeout 300s; proxy_connect_timeout 300s; in Nginx
  3. 3Optimize slow database queries: add indexes, use EXPLAIN to find full table scans
  4. 4Implement async processing: return 202 Accepted and process in background
  5. 5Add backend monitoring: track response times, CPU, memory, and connection pool usage
  6. 6For Cloudflare 504: optimize backend to respond under 100s or upgrade plan for longer timeouts

Tags

http504gateway-timeoutperformancebackend

More in 5xx Server Error

Frequently Asked Questions

In Nginx set proxy_read_timeout. In AWS ALB set idle timeout. Note: Cloudflare free plan has a fixed 100s limit.