nginx still returns 504 although the PHP script has a long time limit
504 Gateway Time-outShort answer
set_time_limit() only controls PHP. nginx has its own clock: if PHP-FPM sends nothing for fastcgi_read_timeout (60 seconds by default), nginx closes the request and shows 504. Raise that value for the one slow location, check PHP's own limits, and move truly long work into a background job.
Fix it fast
1Find out which cause you have
Find which side gave up. The nginx error log says upstream timed out when nginx's own timer ended the request. Compare the time between the request and the error with the numbers below.
sudo grep "upstream timed out" /var/log/nginx/error.log | tail -n 5 sudo nginx -T 2>/dev/null | grep -n "fastcgi_read_timeout"2Match what you see, then jump to its fix
- 1
upstream timed out ... while reading response header from upstream, about 60 seconds after the request startedfastcgi_read_timeout (60 seconds) ends the wait before PHP answersFix - 2
The script dies with a fatal error about maximum execution time, and the PHP log shows itPHP's own time limit stops the script firstFix - 3
The request always dies at the same fixed time, even with set_time_limit(0)A PHP-FPM pool setting kills the workerFix - 4
Even a 5-minute limit is not enough, or users wait on a blank pageThe work is too long for a web requestFix
- 1
What does the error look like?
504 Gateway Time-outupstream timed out (110: Connection timed out) while reading response header from upstream
Cause 1 of 4
fastcgi_read_timeout (60 seconds) ends the wait before PHP answers
1Check
The directive sets how long nginx waits between two reads from the FastCGI server, and the default is 60 seconds. A script that works silently for longer is cut off, whatever set_time_limit() says.
sudo nginx -T 2>/dev/null | grep -n "fastcgi_read_timeout\|fastcgi_pass"2Fix
Raise the timeout in the location that runs the slow script only, not for the whole site.
location ~ ^/reports/export\.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 300s;
}3Verify
Test the config, reload nginx and repeat the slow request.
sudo nginx -t && sudo systemctl reload nginxConfirmed by nginx.org
Cause 2 of 4
PHP's own time limit stops the script first
1Check
The default limit is 30 seconds for web requests, or the max_execution_time value in php.ini. set_time_limit() restarts the counter from the call. On Linux, time spent in database queries and system calls does not count; on Windows it does.
php -r "echo ini_get('max_execution_time'), PHP_EOL;"2Fix
Call set_time_limit() with a realistic value at the top of the slow script, and keep it below the nginx timeout so PHP ends the work cleanly.
<?php
set_time_limit(240); // seconds from this call; 0 removes the limit3Verify
Run the request again and check that the maximum execution time message is gone from your PHP error log.
curl -s -o /dev/null -w '%{http_code} %{time_total}
' https://example.com/reports/export.phpConfirmed by php.net
Cause 3 of 4
A PHP-FPM pool setting kills the worker
1Check
request_terminate_timeout is a pool setting that kills a request after a time. It is off by default (0), so a value here was set by someone.
sudo grep -rn "request_terminate_timeout" /etc/php/8.3/fpm/2Fix
Raise it above the longest allowed request, or remove the line if you want only max_execution_time to apply. Reload PHP-FPM.
; /etc/php/8.3/fpm/pool.d/www.conf
request_terminate_timeout = 300s3Verify
Reload PHP-FPM and repeat the request.
sudo systemctl reload php8.3-fpmConfirmed by php.net
Cause 4 of 4
The work is too long for a web request
1Check
Each running request holds one PHP-FPM worker, and the pool is capped by pm.max_children. A few long exports can fill the pool, so new requests wait.
sudo grep -rn "pm.max_children" /etc/php/8.3/fpm/pool.d/2Fix
Start the job in the background (a queue worker or a cron-run script), return at once with a job id, and let the page poll for the result. Use the slow log to find which scripts are slow.
; /etc/php/8.3/fpm/pool.d/www.conf
request_slowlog_timeout = 10s
slowlog = /var/log/php8.3-fpm.slow.log3Verify
Reload PHP-FPM and read the slow log after the next slow request.
sudo tail -n 40 /var/log/php8.3-fpm.slow.logConfirmed by php.net
How do I stop it coming back?
- Set long timeouts per location, never globally, so a hung script cannot hold every worker.
- Keep PHP's limit a little below the nginx limit, so PHP ends the script cleanly and logs why.
- Move exports, imports and report builds to a background job.
- Turn on the PHP-FPM slow log in production and review it after each release.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Servers
nginx gives up waiting for the app and returns 504
upstream timed out (110: Connection timed out) while reading response header from upstream
nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.
5 causes, each with a fixBack end
PHP stops the script after 30 seconds
Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded
PHP ends a script that runs past max_execution_time (30 seconds by default for web requests).
5 causes, each with a fixServers
nginx and PHP-FPM show File not found or Primary script unknown
File not found.
PHP-FPM received a script path it could not turn into a file.
4 causes, each with a fixServers
nginx cannot open the PHP-FPM socket: Permission denied
502 Bad Gateway
nginx runs as one user and PHP-FPM creates its socket for another.
3 causes, each with a fixServers
Cookies from a PHP backend vanish behind an nginx proxy
Login works when you call the backend directly but fails through nginx
nginx forwards Set-Cookie headers from the backend unchanged, so lost cookies usually come from the cookie itself: a domain or path that does not match the public URL, or Secure and SameSite flags the browser rejects.
4 causes, each with a fixServers
display_errors has no effect on PHP behind nginx and PHP-FPM
500 Internal Server Error with no PHP message in the browser
With nginx there is no web-server PHP setting.
4 causes, each with a fix