Apache returns 504 Gateway Timeout when PHP takes too long
504 Gateway TimeoutShort answer
A 504 from Apache means the backend did not answer within the proxy timeout. With a PHP backend behind mod_proxy, the request is either slower than ProxyTimeout or the timeout set on ProxyPass, or every PHP worker is busy so the request waits. Find the slow request first, then raise the timeout or the worker count.
Fix it fast
1Find out which cause you have
See which timeout is configured for the proxy and what the Apache error log says at the moment of the 504.
grep -rn "ProxyTimeout\|timeout=" /etc/apache2/ sudo tail -n 30 /var/log/apache2/error.log2Match what you see, then jump to its fix
What does the error look like?
504 Gateway TimeoutQuick requests work, slow ones end in a 504 after a fixed wait
Cause 1 of 2
The request is slower than the proxy timeout
1Check
ProxyTimeout is the network timeout for proxied requests and defaults to the value of Timeout. A timeout= parameter on ProxyPass overrides it for that backend and sets how long Apache waits for data from it.
grep -rn "ProxyTimeout\|ProxyPass\|ProxySet" /etc/apache2/2Fix
If the work is legitimately slow (an export, a report), raise the timeout for that location only. If it should be fast, find out why it is slow instead.
ProxyPass "/reports" "http://backend.example.com/reports" timeout=120
# or for the whole server
ProxyTimeout 1203Verify
Test the config, reload Apache and time the slow request.
sudo apachectl configtest && sudo systemctl reload apache2
curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://example.com/reportsConfirmed by httpd.apache.org
Cause 2 of 2
Every PHP-FPM worker is busy, so new requests queue
1Check
pm.max_children caps how many requests PHP-FPM serves at the same time. Once all children are busy, further requests wait. Turn on the slow log to see which scripts hold workers.
grep -rn "pm.max_children\|request_slowlog_timeout" /etc/php/8.3/fpm/pool.d/2Fix
Log slow scripts with request_slowlog_timeout, fix or move them out of the web path, and raise pm.max_children only as far as memory allows.
; pool file
request_slowlog_timeout = 5s
slowlog = /var/log/php8.3-fpm.slow.log
pm.max_children = 203Verify
Reload PHP-FPM, repeat the load and check the slow log names a script.
sudo systemctl reload php8.3-fpm
sudo tail -n 30 /var/log/php8.3-fpm.slow.logConfirmed by php.net
How do I stop it coming back?
- Move reports, imports and other long jobs to a queue and answer the web request at once.
- Keep request_slowlog_timeout on so a slow script is named before users complain.
- Give a long route its own ProxyPass with a larger timeout instead of raising the limit for the whole site.
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:
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 fixServers
Composer reports a different PHP version than php -v on Ubuntu
You are running 8.1.2.
Composer takes its PHP version from the interpreter that runs it, unless config.platform fakes one.
3 causes, each with a fixServers
docker-php-ext-install mbstring fails: oniguruma not found
The docker-php-ext-install step exits with a non-zero code
mbstring needs the Oniguruma library to build its regex functions, and the PHP build finds it through pkg-config.
3 causes, each with a fixServers
Laravel returns 500 Internal Server Error on Apache
/var/www/html/.htaccess: RewriteCond: bad flag delimiters
Apache and Laravel report different 500s.
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 still returns 504 although the PHP script has a long time limit
upstream timed out (110: Connection timed out) while reading response header from upstream
set_time_limit() only controls PHP.
4 causes, each with a fix