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
- 1Identify slow requests: check backend access logs for response times over 30s
- 2Increase proxy timeout: proxy_read_timeout 300s; proxy_connect_timeout 300s; in Nginx
- 3Optimize slow database queries: add indexes, use EXPLAIN to find full table scans
- 4Implement async processing: return 202 Accepted and process in background
- 5Add backend monitoring: track response times, CPU, memory, and connection pool usage
- 6For Cloudflare 504: optimize backend to respond under 100s or upgrade plan for longer timeouts
Tags
http504gateway-timeoutperformancebackend
More in 5xx Server Error
http-500-internal-server-errorHTTP 500 Internal Server Error — What It Means & How to Fix It
Criticalhttp-501-not-implementedHTTP 501 Not Implemented — What It Means & How to Fix It
Criticalhttp-502-bad-gatewayHTTP 502 Bad Gateway — What It Means & How to Fix It
Criticalhttp-503-service-unavailableHTTP 503 Service Unavailable — What It Means & How to Fix It
Criticalhttp-504-gateway-timeoutHTTP 504 Gateway Timeout — What It Means & How to Fix It
Criticalhttp-505-http-version-not-supportedHTTP 505 HTTP Version Not Supported — What It Means & How to Fix It
CriticalFrequently Asked Questions
In Nginx set proxy_read_timeout. In AWS ALB set idle timeout. Note: Cloudflare free plan has a fixed 100s limit.