What the message looks like
2026/10/29 09:14:02 [error] 1234#1234: *5678 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 203.0.113.5, server: example.com, request: "POST /api/export HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock"Common causes
- The request is simply slow: a long database query, an export, a report or a call to a slow external API.
- All PHP-FPM workers are busy (
pm.max_childrenreached) and new requests wait in the queue. - The default
fastcgi_read_timeoutorproxy_read_timeoutof 60 seconds is shorter than the task needs. - The backend hangs, for example in a deadlock or while waiting for a locked file.
- For “while connecting to upstream”: a firewall or network problem between nginx and the backend host.
Check
- Which part timed out? Read the end of the log line
grep -i 'timed out' /var/log/nginx/error.log | tail -n 20 - Are all PHP-FPM workers busy?
grep -i 'max_children' /var/log/php8.3-fpm.log | tail -n 5 - How long does the backend need for the slow URL?
curl -s -o /dev/null -w '%{time_total}s\n' https://example.com/api/export - Is the backend reachable at all?
systemctl status php8.3-fpm
Fix
- Find out why the request is slow (database, external API) and fix that first. A longer timeout only hides the problem.
- If the task really needs more time, raise the limit for that location only:
fastcgi_read_timeout 300;orproxy_read_timeout 300;. - Raise
pm.max_childrenin the PHP-FPM pool if the workers are the bottleneck and the RAM allows it. - Move long jobs to the background (cron, queue) and let the page return at once.
- For “connecting to upstream”: check the firewall and the address of the backend.
Typical scenario
In the WordPress admin you start a WooCommerce product export. After exactly 60 seconds the browser shows "504 Gateway Time-out", and the nginx error log says upstream timed out (110) while reading response header from upstream. The export worked with 500 products; now there are 8000. Look at the access log first: with $request_time and $upstream_response_time in the log_format you see which URLs are really slow.
FAQ
I raised the timeout in nginx but users still see an error. Why?
Other layers have their own limits: PHP's max_execution_time, a load balancer, or a CDN. Cloudflare, for instance, closes the connection after about 100 seconds.
Is 504 the same as 502?
No. 502 means the backend gave an invalid answer or was not reachable. 504 means it did not answer in time.
Which timeout applies to me, fastcgi_read_timeout or proxy_read_timeout?
It depends on how nginx hands the request to the backend. With fastcgi_pass (PHP-FPM) it is fastcgi_read_timeout, with proxy_pass it is proxy_read_timeout. Both measure the pause between two reads, not the total duration of the request.
How do I measure how long the backend really takes?
Add $request_time and $upstream_response_time to your log_format and sort the access log by the largest values. That shows whether a few URLs are outliers or everything is slow, before you raise any timeout.
Does the script keep running in the backend after nginx closed the connection?
Often yes. nginx only closes its own connection to the backend; PHP-FPM may let the script run until it finishes or hits max_execution_time. An export can therefore still complete in the background, and clicking "retry" starts it a second time.