Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Apache returns 504 Gateway Timeout when PHP takes too long

Error504 Gateway Timeout

Short 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.

  • 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

    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.log
  2. 2Match what you see, then jump to its fix

    1. 1The 504 appears after the same number of seconds every time, and that number matches ProxyTimeout or a timeout= valueThe request is slower than the proxy timeoutFix
    2. 2Fast pages time out only while a few slow requests are runningEvery PHP-FPM worker is busy, so new requests queueFix

What does the error look like?

  • 504 Gateway Timeout
  • Quick 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 120

3Verify

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/reports

Confirmed by httpd.apache.org

Did this fix it?

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 = 20

3Verify

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.log

Confirmed by php.net

Did this fix it?

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 fix

Next step

Want this handled for good?

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