Nginx 504 Gateway Timeout: causes and fixes
Short answer
504 means the backend did not answer within nginx's timeout. Find what is slow first (queries, external APIs, queues), then raise proxy_read_timeout or fastcgi_read_timeout only as needed.
Most common cause: Slow backend work: slow SQL, a hung external API, long exports/uploads.
Try this first: Longer timeouts only where needed
location /api/export {
proxy_pass http://127.0.0.1:3000;
proxy_connect_timeout 5s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}Last verified: · against the official docs listed below · Sources
Typical log lines
upstream timed out (110: Connection timed out) while reading response header from upstream upstream timed out (110: Connection timed out) while connecting to upstream
Common causes
- Slow backend work: slow SQL, a hung external API, long exports/uploads.
- PHP-FPM pool exhausted (pm.max_children too low) so requests queue.
- proxy_read_timeout / fastcgi_read_timeout too short (default 60s).
- Network path to the upstream is broken, so the connect phase times out.
Diagnosis commands
grep 'upstream timed out' /var/log/nginx/error.log | tailtime curl -s -o /dev/null http://127.0.0.1:3000/slow-pathgrep -i 'max_children' /var/log/php*-fpm.logFix configs
Longer timeouts only where needed
location /api/export {
proxy_pass http://127.0.0.1:3000;
proxy_connect_timeout 5s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
}PHP-FPM
location ~ \.php$ {
fastcgi_read_timeout 300s;
...
}
# also tune max_execution_time in php.ini and pm.max_children in FPMRun nginx -t first, then systemctl reload nginx.
Key facts and sources
- proxy_read_timeout defaults to 60s and applies between two successive reads, not to the whole response. [nginx.org]
- fastcgi_read_timeout also defaults to 60s. [nginx.org]
Checked against the linked pages on 2026-11-14; the official docs win if a newer nginx changes them.
FAQ
Can I just set a huge timeout?
It stops the bleeding but not the cause. Long-held connections tie up workers; long jobs belong in an async queue with polling.
Is Cloudflare 524 the same as 504?
Similar. 524 is Cloudflare waiting over 100 s (free plan) for your origin; raising nginx timeouts will not help, the request must finish within 100 s.
Fix it with a tool
Related errors
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.