Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Apache returns 503 or 504 because PHP-FPM did not answer in time

ErrorThe timeout specified has expired

Short answer

Apache sent the request to PHP-FPM and gave up waiting. Either the script runs longer than Apache's proxy timeout, every PHP-FPM worker is busy so the request just waits, or PHP-FPM kills the request itself. The Apache error log and the PHP-FPM slow log tell these apart.

  • Applies to Apache 2.4, PHP-FPM 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the failing requests in the Apache error log and note whether they are always the same URL (a slow script) or random ones under traffic (a busy pool).

    sudo grep -E 'proxy_fcgi|timeout specified' /var/log/apache2/error.log | tail -n 20
  2. 2Match what you see, then jump to its fix

    1. 1Always the same URL fails, after about the same number of secondsThe script takes longer than Apache waitsFix
    2. 2Random URLs time out when traffic is high, while a single request on a quiet server worksAll PHP-FPM workers are busyFix
    3. 3Failures happen at a round number of seconds that matches a PHP-FPM settingPHP-FPM ends the request before Apache doesFix

What does the error look like?

  • The timeout specified has expired
  • Error dispatching request
  • proxy_fcgi:error

Cause 1 of 3

The script takes longer than Apache waits

1Check

Apache waits for data from the backend for ProxyTimeout seconds, which defaults to the value of Timeout. A report or import that runs longer than that is cut off on the Apache side. Time the request.

grep -R -n -E "ProxyTimeout|^Timeout|timeout=" /etc/apache2/
time curl -s -o /dev/null https://example.com/slow-report.php

2Fix

First make the script faster or move it to a queue. If it must stay long, raise the timeout for that backend only, with the timeout parameter on the proxy worker or ProxyTimeout.

# in the vhost, next to the PHP handler
ProxyTimeout 120

3Verify

Test the config, reload Apache and repeat the slow request.

sudo apachectl configtest && sudo systemctl reload apache2
time curl -s -o /dev/null https://example.com/slow-report.php

Confirmed by httpd.apache.org

Did this fix it?

Cause 2 of 3

All PHP-FPM workers are busy

1Check

Each PHP-FPM worker serves one request at a time. When pm.max_children workers are all busy, new requests queue until Apache gives up. The Apache docs also warn that idle persistent connections can occupy every worker. See the pool settings and whether connection reuse is on.

sudo grep -R -n -E "^pm\.|^pm =" /etc/php/8.3/fpm/pool.d/
grep -R -n "enablereuse" /etc/apache2/

2Fix

Raise pm.max_children to what your memory allows, and turn off enablereuse on the FPM worker if it is on and the pool is small.

; /etc/php/8.3/fpm/pool.d/www.conf
pm.max_children = 20

3Verify

Restart PHP-FPM and repeat a load test; the timeouts should stop appearing.

sudo systemctl restart php8.3-fpm
sudo grep -c "timeout specified" /var/log/apache2/error.log

Confirmed by httpd.apache.org

Did this fix it?

Cause 3 of 3

PHP-FPM ends the request before Apache does

1Check

request_terminate_timeout kills a worker after that many seconds. It is 0 (off) by default, so only a value someone set explains this. request_slowlog_timeout, when set, writes a backtrace of slow requests so you can see where the time goes.

sudo grep -R -n -E "request_terminate_timeout|request_slowlog_timeout|^slowlog" /etc/php/8.3/fpm/

2Fix

Raise or remove request_terminate_timeout for the pool, and keep max_execution_time in php.ini consistent with it.

; pool config
request_terminate_timeout = 120s
request_slowlog_timeout = 10s

3Verify

Restart PHP-FPM and run the long request again.

sudo systemctl restart php8.3-fpm

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Move work that takes more than a few seconds into a queue and return at once.
  • Set request_slowlog_timeout so slow requests leave a backtrace before they become timeouts.
  • Size pm.max_children from memory per worker, not by guess.
  • Keep Apache's ProxyTimeout and PHP-FPM's request_terminate_timeout in step.

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

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.