HTTP Status Error
HTTP 504 Gateway Timeout
The gateway or reverse proxy server (such as Nginx, Cloudflare, or AWS ALB) did not receive a timely response from an upstream server.
What This Error Means
A 504 Gateway Timeout indicates that a server acting as a gateway or proxy did not receive a response from an upstream server within its configured timeout window.
Unlike a 502 (where the backend returns an invalid or broken response), a 504 means the backend simply took too long to respond.
Why It Occurs
Slow database queries or unindexed queries blocking backend application threads.
Heavy computation or unoptimized external API calls running synchronously during HTTP request handling.
Upstream service connection pool exhaustion causing requests to queue up indefinitely.
Network latency or packet loss between proxy and upstream application host.
Symptoms
- ⚠ Browser displays 504 Gateway Time-out after waiting 30, 60, or 120 seconds
- ⚠ Nginx logs show "upstream timed out (110: Connection timed out) while reading response header from upstream"
- ⚠ High PHP-FPM, Python WSGI, or Node.js CPU utilization
Common Causes
- • Backend query executing longer than proxy timeout setting (proxy_read_timeout in Nginx)
- • Third-party external API integration hanging or failing to return in a timely manner
- • Resource contention causing server execution lag during peak traffic spikes
How to Fix It
- Profile and optimize slow SQL queries using EXPLAIN ANALYZE
- Offload long-running operations (PDF generation, bulk emails, data export) to asynchronous worker queues (Celery, BullMQ, Redis)
- Temporarily increase proxy timeouts in Nginx: proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 120s;
- Configure connection pooling and scale horizontal application replicas
| Command | Purpose |
|---|---|
| top -b -n 1 | Check server CPU and memory usage |
| tail -n 50 /var/log/nginx/error.log | Inspect reverse proxy timeout entries |
| journalctl -u myapp --since "15m ago" | Check backend application response latency |
Verification
- ✓ Verify slow endpoint responds within acceptable thresholds using curl -o /dev/null -s -w "%{time_total}\n" https://yourdomain.com/api
- ✓ Confirm background queues process heavy tasks without blocking HTTP threads
Prevention
- → Enforce strict HTTP client timeouts (e.g. 5-10s) on all outbound third-party API calls
- → Set up database query monitoring and slow query alerts in production