Fix "upstream timed out (110: Connection timed out)" and 504 Gateway Time-out in Nginx
2026/11/28 14:03:27 [error] 1234#1234: *89 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 203.0.113.7, server: example.com, request: "GET /api/report HTTP/1.1", upstream: "http://127.0.0.1:3000/api/report", host: "example.com"
- 504 Gateway Time-out
- upstream timed out (110: Connection timed out) while reading response header from upstream, upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"
- upstream timed out (110: Connection timed out) while connecting to upstream
- WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
- HTTP/1.1 504 Gateway Timeout
Nginx did reach your application (PHP-FPM, Node, Python…), but it did not answer within the allowed time (60 seconds by default), so Nginx returns 504. The real fix is to find and speed up the slow request; raising proxy_read_timeout or fastcgi_read_timeout is only a complement for legitimate long jobs.
What this error means
Nginx acts as a proxy: it forwards the request to the "upstream" server (your application) and waits for the header of its response. If nothing arrives before proxy_read_timeout (HTTP) or fastcgi_read_timeout (PHP-FPM), both 60 seconds by default, it gives up, writes upstream timed out (110: Connection timed out) to error.log and sends the browser a "504 Gateway Time-out" page. Unlike a 502, the application is reachable: it is just too slow, stuck, or so overloaded that the request waits in line. The while connecting to upstream variant means even the connection could not be established in time (firewall, wrong IP).
Common causes
- Genuinely slow request: SQL query without an index, slow external API call, export or PDF generation done during the HTTP request.
- Saturated application: every PHP-FPM worker is busy (
server reached pm.max_children) or the Node event loop is blocked by synchronous work, and requests pile up. - Inconsistent timeouts between layers: Nginx cuts at 60 s while PHP (
max_execution_time) or the load balancer in front allows more, or the other way round. - Database-side blocking: a lock, a transaction never committed or an exhausted connection pool; the application waits doing nothing.
- Unreachable upstream (
while connecting to upstream): firewall, security group or wrong private IP between Nginx and the application.
Step-by-step fix
-
Read the log and spot the slow URL
The
request:field gives the URL andupstream:the application involved. Then measure the response time directly on the application, bypassing Nginx.sudo tail -n 50 /var/log/nginx/error.log | grep "upstream timed out" curl -s -o /dev/null -w "status=%{http_code} total=%{time_total}s\n" http://127.0.0.1:3000/api/report -
Check the application is not saturated
For PHP-FPM, look for the
pm.max_childrenwarning and enable the slow log, which prints the call stack of slow requests. For Node, check the process CPU: 100% on one core means a blocked event loop.sudo grep -i "max_children" /var/log/php8.3-fpm.log # /etc/php/8.3/fpm/pool.d/www.conf request_slowlog_timeout = 5s slowlog = /var/log/php-fpm-slow.log -
Fix the real cause
Add the missing index, cache the result, or move the long job out of the HTTP request: a queue (Laravel Queues, BullMQ, Celery), then a notification or polling when it is ready. A web request should answer in a few seconds at most.
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 42 ORDER BY created_at DESC; CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at); -
Tune the workers if the server allows it
If every worker is busy but memory is available, raise
pm.max_children(roughly free memory / average worker memory), then restart PHP-FPM.; /etc/php/8.3/fpm/pool.d/www.conf pm = dynamic pm.max_children = 20 pm.start_servers = 4 pm.min_spare_servers = 2 pm.max_spare_servers = 6 -
Raise the timeout for one route
For a legitimate long job (report, import), raise the timeout only on that route, and align the other layers: PHP
max_execution_time, PHP-FPMrequest_terminate_timeout, and the load balancer or Cloudflare timeout in front (100 s maximum on standard plans).location /api/report { proxy_pass http://127.0.0.1:3000; proxy_read_timeout 300s; proxy_send_timeout 300s; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_read_timeout 300s; } -
Test and reload Nginx
nginx -tvalidates the configuration before applying it;reloaddoes not drop current connections.sudo nginx -t && sudo systemctl reload nginx sudo systemctl restart php8.3-fpm
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Monitor route response times (p95) and busy worker counts: a 504 rarely arrives without warning signs.
- Run any job longer than a few seconds in a queue, never inside the HTTP request.
- Document the timeout of each layer (CDN, load balancer, Nginx, PHP) and keep them consistent: the shortest one always wins.
Frequently asked questions
What is the difference between a 502 and a 504?
502 Bad Gateway: Nginx could not talk to the application (process down, wrong port or socket). 504 Gateway Time-out: it reached it but the answer came too late.
I set proxy_read_timeout to 600s and still get a 504, why?
Another layer cuts first: load balancer (60 s by default on AWS ALB), Cloudflare (error 524 after 100 s), or the directive is in the wrong block. For PHP-FPM, fastcgi_read_timeout is what counts, not proxy_read_timeout.
The 504 only happens at peak hours, what should I do?
That is a sign of saturation: requests wait for a free worker. Profile the most called routes, add caching and workers, or spread the load over several instances.
Useful tools for this error
cURL converter
Paste a cURL command (DevTools “Copy as cURL”): get the fetch, axios, Node, PHP (Laravel Http or curl) or Python requests code.
HTTP status codes
All the common HTTP codes (401, 403, 404, 419, 429, 500, 502…): meaning, causes and what to do.
Error finder
Paste an error message or a log: the tool recognises the error and opens the right fix.
More DevOps, servers & cloud errors
- Fix "nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)"
- Fix "413 Request Entity Too Large" (Nginx, PHP, Express, Cloudflare)
- Fix "Certbot failed to authenticate some domains" (Let’s Encrypt, Timeout during connect, acme-challenge 404)
- Fix "Resource not accessible by integration" in GitHub Actions
- Fix the 404 on refresh of a SPA on Vercel ("404: NOT_FOUND"), Netlify and Nginx
- Fix "An error occurred (AccessDenied) when calling the PutObject operation" on AWS S3
- Fix "The connection to the server localhost:8080 was refused" with kubectl