PHP-FPM 502 and 504 errors behind nginx: find the cause and fix it

nginx returns 502 or 504 when PHP-FPM is down, out of workers or too slow. How to read the logs, tune pm.max_children and use the slow log to find the culprit.

· 6 min read · By the Spot Downtime team

On a typical PHP site, nginx serves static files and passes .php requests to PHP-FPM, a pool of worker processes. When that hand-off fails, visitors see 502 Bad Gateway or 504 Gateway Timeout. WordPress, Laravel, Drupal and Magento sites all hit this.

The status code tells you which kind of failure it is. This post walks from the symptom to the setting that fixes it.

What each status means here

  • 502: nginx could not get a valid answer from PHP-FPM. FPM is not running, the socket path or permissions are wrong, or a worker crashed in the middle of the request.
  • 504: nginx reached PHP-FPM, but the response took longer than fastcgi_read_timeout, which defaults to 60 seconds. The script is slow, stuck, or waiting for a free worker.

The general version of this diagnosis is in 502, 503 and 504 errors. Start with the logs.

Step 1: read the nginx error log

bash
sudo tail -n 50 /var/log/nginx/error.log

The message narrows it down quickly:

  • connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory): FPM is not running, or nginx points to the wrong socket path.
  • connect() ... failed (13: Permission denied): the nginx user cannot open the socket. Check listen.owner and listen.group in the pool file.
  • connect() ... failed (111: Connection refused): you use a TCP address such as 127.0.0.1:9000 and nothing is listening there.
  • upstream prematurely closed connection while reading response header: a worker died, often from a PHP crash or the memory limit.
  • upstream timed out (110: Connection timed out) while reading response header: the 504 case, covered in nginx upstream timed out.

Step 2: confirm FPM is running

bash
sudo systemctl status php8.3-fpm
sudo journalctl -u php8.3-fpm --since "1 hour ago"
# Validate the configuration before restarting
sudo php-fpm8.3 -t

The service name varies: php-fpm, php8.3-fpm or php-fpm83. If the service keeps stopping, the system may be killing it for memory, which shows up in dmesg as an OOM kill. See Linux out of memory.

Step 3: check for "server reached pm.max_children"

The most common cause under load is running out of workers. Look in the FPM log (for example /var/log/php8.3-fpm.log) for:

text
WARNING: [pool www] server reached pm.max_children setting (5), consider raising it

When every worker is busy, new requests wait in the queue until nginx times out. Raising pm.max_children helps, but only up to the memory you have. Estimate it:

  1. Measure the memory of one worker: ps --no-headers -o rss -C php-fpm8.3 | sort -n | tail (values are in KB).
  2. Subtract memory used by the database, nginx and the system from total RAM.
  3. Divide what is left by the average worker size.

For example, 2 GB of RAM with about 1 GB left for PHP and workers averaging 60 MB gives roughly 16 workers. A setting well above that invites the out-of-memory killer, and then the 502 returns in a worse form.

ini
; /etc/php/8.3/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 16
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500

pm.max_requests recycles each worker after that many requests, which limits the damage from slow memory leaks. Reload FPM after changes: sudo systemctl reload php8.3-fpm.

Step 4: find the slow scripts

More workers only hides a slow script. To find the script, enable the FPM slow log, which writes a stack trace for any request running longer than a threshold:

ini
request_slowlog_timeout = 5s
slowlog = /var/log/php8.3-fpm-slow.log

The trace shows which function was running, often a database query, an HTTP call to a third-party API with no timeout, or a lock. Fix those and the workers free up on their own. Set timeouts on outbound calls so one slow dependency cannot hold every worker.

Step 5: align the timeouts

Three limits interact and should be set on purpose:

  • max_execution_time in php.ini: how long PHP runs a script before stopping it.
  • request_terminate_timeout in the pool file: FPM kills the worker after this long, regardless of PHP settings.
  • fastcgi_read_timeout in nginx: how long nginx waits for a response.
nginx
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 60s;
}

Don't just raise the timeout

Raising fastcgi_read_timeout turns a 504 into a slower page, and slow requests keep workers busy, which makes the next 504 more likely. Raise it only for known long jobs such as an export or import, and move those to a background queue when you can.

If you use WordPress

Plugins that call external APIs on page load and heavy admin pages are the usual cause of long requests. Disable plugins one at a time to isolate the culprit. If PHP itself crashes with a blank page, see the WordPress white screen of death.

Catch it before customers do

These errors are often intermittent: they appear at traffic peaks and clear by themselves. A website monitor that checks your PHP pages catches the 502s and 504s as they occur, and failures are confirmed from several regions before an alert is sent. Point it at a dynamic page, not only a cached static one, because a CDN can keep serving cached pages while the PHP backend is down. A server monitor that tracks memory and CPU shows the pressure building before the workers run out.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading