Web Infrastructure & Cloud APIHigh Severity
504 Gateway Timeout: Fix Server Upstream Timeout & Database Bottlenecks
Applies to: Nginx, Cloudflare, AWS ALB, Node.js
Safety & Usage Advisory
Can you still drive / use device? Client web request timed out before the backend responded.
Estimated Repair Cost
$0
$0 (DevOps & Server Optimization)
Diagnostic Category
Web Infrastructure & Cloud API
Nginx, Cloudflare, AWS ALB, Node.js
Fix Difficulty
Beginner
~30-60 Mins Required
Common Symptoms
- Browser displays '504 Gateway Timeout'
- Cloudflare 504 Origin Timeout error screen
- Long-running API endpoints aborting after 30s / 60s
Root Causes
- Unindexed heavy SQL database queries blocking backend threads
- Third-party microservice or payment API failing to reply
- Nginx `proxy_read_timeout` configured too low for heavy exports
Step-by-Step Instructions to Fix HTTP 504
1
Increase Nginx Proxy Timeout Directives
In nginx.conf: set `proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300;`.
2
Analyze Slow Database Query Logs
Check PostgreSQL `pg_stat_activity` or MySQL slow query log for unindexed table locks.
3
Offload Long Jobs to Background Queues
Convert long-running export/crawl tasks into asynchronous jobs with BullMQ or Celery.
How to Prevent HTTP 504 in the Future
- Always implement asynchronous worker queues for any web tasks taking longer than 5 seconds
HTTP 504 Troubleshooting FAQs
502 Bad Gateway means the upstream server sent an invalid or crashed response. 504 Gateway Timeout means the upstream server didn't respond in time.
