The error string in nginx error.log:
*1234 connect() failed (111: Connection refused) while connecting to upstream, client: 10.0.0.5, server: example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:"
errno 111 is ECONNREFUSED. nginx tried to open a connection to the FastCGI backend and the kernel on the PHP-FPM side rejected it before any FastCGI bytes were exchanged. Users see HTTP 502 Bad Gateway in batches.
Four failure modes present identically from nginx’s seat: the PHP-FPM master is not running; the configured socket path or TCP address does not match what nginx is dialing; the kernel dropped the connection because the listen backlog is full; or the socket exists and paths match, but something environmental prevents nginx from reaching it (private tmp namespace, container network boundary, IPv6/IPv4 mismatch, socket permissions). One read-only check on the FPM host disambiguates all four: is anything actually listening on the address nginx is trying to reach?
This page covers the link-layer refusal. If nginx can connect but the request never finishes in time, see the related 504 guide.
What this means
connect() is the syscall nginx issues against the address in fastcgi_pass. ECONNREFUSED is returned synchronously by the kernel when:
- the destination address has no listener bound to it (no process called
listen()on it), or - the listener’s accept queue (the backlog) is full and the kernel’s policy for that socket is to refuse rather than silently drop.
For a Unix socket, an absent socket file usually produces ENOENT (“no such file or directory”) rather than ECONNREFUSED, but nginx reports both under the same connect() failed prefix. A refused Unix socket typically means the file exists but no process is accepting on it (master died, stale socket left behind) or the path resolves into a different mount namespace from nginx’s perspective. For TCP, ECONNREFUSED with the port closed and ECONNREFUSED with the backlog full look identical to nginx.
The diagnostic that matters: separate a dead listener from a saturated one. A dead listener has zero capacity and refuses every connection. A saturated listener is technically up, the ping endpoint may still answer, and only connections that arrive faster than workers can accept() are refused. Mixing these two leads to the wrong fix (raising pm.max_children on a pool that never started).
flowchart TD
A["nginx error.log
connect() failed (111)"] --> B{"Master running?
pgrep php-fpm"}
B -- No --> C["FPM down or crashed
journalctl, dmesg"]
B -- Yes --> D{"Socket listening?
ss -lnp"}
D -- No --> E["listen path/address wrong
or bind failed"]
D -- Yes --> F{"nginx fastcgi_pass
matches listen?"}
F -- No --> G["Config drift between
nginx and pool"}
F -- Yes --> H{"Backlog full?
Recv-Q at limit"}
H -- Yes --> I["Worker saturation
refused at kernel"]
H -- No --> J["Environmental block:
PrivateTmp, container net,
IPv6, perms, SELinux"]Common causes
| Cause | What it looks like | First thing to check |
|---|---|---|
| Master not running | No php-fpm: master process in ps; socket file absent or stale; ping fails 100% | systemctl status php-fpm, journalctl -u php-fpm -n 50 |
| Wrong socket path or address | Master is up and listening, but on a different path/port than fastcgi_pass dials | Compare php-fpm -tt listen against nginx -T fastcgi_pass |
| Backlog overflow | Master up, socket listening, ping may still answer, but kernel refuses bursts | Kernel counters ListenOverflows/ListenDrops rising; FPM listen queue at listen queue len |
| Environmental block | Socket present, paths match, nginx still refused | PrivateTmp namespace, container network, IPv6 resolution, socket ownership/mode, SELinux AVCs |
Quick checks
# Is the master alive? expect one master plus N workers
pgrep -af php-fpm
# What is FPM actually listening on? (-p needs root for the pid column)
ss -lnp | grep -E 'php|9000'
# What does nginx dial? confirm the fastcgi_pass target
nginx -T 2>/dev/null | grep -i fastcgi_pass
# What does the pool config say it listens on? (-tt parses and dumps effective config, no daemon)
php-fpm -tt 2>&1 | grep -E 'listen\s*=|listen\.backlog'
# Does the socket file exist with the expected owner/mode?
ls -l /run/php /var/run/php* 2>/dev/null
# Is the backlog currently full? Recv-Q on the LISTEN line vs the configured backlog
ss -tlnp | grep -E '9000|php'
ss -xlnp | grep php
# Kernel-side refusals since boot (cumulative counters)
nstat -z 2>/dev/null | grep -iE 'ListenOverflows|ListenDrops'
# FPM's own view of saturation
curl -s http://127.0.0.1/fpm-status | grep -E 'listen queue|active processes|max children'
# Recent FPM errors and any reload window
journalctl -u php-fpm --since "30 min ago" --no-pager | tail -50
All of these are read-only.
How to diagnose it
Confirm whether the master is running.
pgrep -af php-fpmshould show onemaster processline plus worker lines. If it shows nothing, the refusal is a dead listener, not saturation. Go to the master-down path below.Confirm something is listening on the address nginx dials. Copy the
fastcgi_passvalue out ofnginx -Tand checkss -lnpfor that exact path orip:port. For a Unix socket, the path must match character for character. For TCP, the bound address family matters: a listener on127.0.0.1:9000will not satisfyfastcgi_pass [::1]:9000.Cross-check paths between nginx and the pool. Run
php-fpm -ttand read thelisten =line for thewwwpool (or whichever pool nginx targets). The two values must agree. A common drift source is a distro upgrade that moves sockets from/var/run/php5-fpm.sockto/run/php/php8.2-fpm.sockwhile the nginx vhost is never updated.If the listener is up and paths match, check the backlog. Look at
ss -tlnp(TCP) orss -xlnp(Unix). TheRecv-Qcolumn on theLISTENline is the current depth of the accept queue. IfRecv-Qsits at the configuredlisten.backlog(ornet.core.somaxconn, whichever is smaller) and refuses to drain, the kernel is refusing new connections. Cross-check withnstat -z | grep -iE 'ListenOverflows|ListenDrops': a rising counter confirms kernel-level refusals. This is a saturation problem, not a connectivity problem.If none of the above apply, look for an environmental block.
ls -lthe socket file and confirm the nginx worker user has read/write access. On systemd hosts, check whetherphp-fpm.serviceruns withPrivateTmp=true(systemctl show php-fpm -p PrivateTmp): a socket under/tmpor/var/tmpis invisible to nginx in a different tmp namespace. In containers, confirm the FPM container is listening on0.0.0.0:9000or its bridge IP, not127.0.0.1:9000, which is unreachable from the nginx container’s loopback. On SELinux hosts,ausearch -m AVC -ts recentwill surface denials on the socket path.
The single question that splits the investigation cleanly: does the FPM status page respond while nginx is logging 502s? If yes, the pool is alive and you are looking at saturation or an environmental block. If no, the master is down or the socket is unreachable.
Metrics and signals to monitor
| Signal | Why it matters | Warning sign |
|---|---|---|
| nginx upstream 5xx rate | First user-visible symptom of a refused connection | Sustained non-zero 502 rate on PHP locations |
Master process alive (pgrep, ping endpoint) | Distinguishes dead listener from saturated listener | Ping fails 100%, or pgrep php-fpm empty |
PHP-FPM listen queue and listen queue len | Saturation is visible here before kernel refusals | listen queue approaches listen queue len |
active processes / max_children | Precondition for backlog growth | Sustained at 1.0 with traffic present |
Recv-Q on FPM listen socket (ss) | Kernel-level confirmation of accept queue depth | Pegged at configured backlog |
TcpExtListenOverflows / TcpExtListenDrops | Cumulative count of kernel-level refusals invisible to FPM | Counter increasing during the incident window |
| Socket file existence and ownership | Catches stale sockets and permission drift after upgrades | File missing, or mode/owner denies nginx user |
Fixes
Master not running
Find out why before restarting. The refusal is the symptom; the master died or never started for a reason. Pull journalctl -u php-fpm -n 100 for the startup error. Common causes: a pool config syntax error after a deploy, a missing PHP extension pulled in by new code, the error log directory not being writable, or an OOM kill of the master visible in dmesg. Validate the config before bringing the service back:
# Config-test only; exits non-zero on syntax errors with details
php-fpm -tt
Restarting as the first action hides the root cause and gives you the same outage again on the next deploy.
Wrong socket path or address
Align the two configs. Set listen = in the pool to a stable path under /run/php/ (or /var/run/php/, which is the same directory on modern distros) and point fastcgi_pass at exactly that path. For TCP, ensure both sides agree on address family and port. For Unix sockets, also set the ownership and mode so the nginx worker user can read and write:
listen = /run/php/php8.2-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
The listen.owner and listen.group directives must match the user nginx workers run as. Wrong ownership produces EACCES (“permission denied”) in nginx, which reports under the same connect() failed prefix but with a different errno (13).
Backlog overflow (saturation)
This is not a connection problem; it is a capacity problem that surfaces as a connection problem. Do not raise listen.backlog as the fix. A deeper queue just delays the refusal by seconds and hides the real signal. The actual fixes are: raise pm.max_children if memory headroom exists, or remove the slow dependency that is holding workers hostage. The saturation pattern, the slow-log-driven root cause, and the memory math for raising max_children are covered in the related guides below.
One configuration note: listen.backlog defaults changed across PHP versions. PHP 8.2 and later default to -1 on Linux, which the kernel clamps to net.core.somaxconn (commonly 4096 on modern kernels). PHP before 8.2 defaults to 511. If you rely on the default, the effective backlog depends on both the PHP version and the kernel sysctl.
Environmental blocks
- systemd PrivateTmp: if
php-fpm.servicehasPrivateTmp=trueand the pool listens on a socket under/tmpor/var/tmp, nginx cannot see that socket. Move the socket under/run/php/(preferred) or override with a drop-in settingPrivateTmp=false. - Container networking: a PHP-FPM container listening on
127.0.0.1:9000is unreachable from the nginx container’s loopback. The FPM pool must listen on0.0.0.0:9000or the container’s bridge IP, and nginx mustfastcgi_passto the FPM container’s service name or IP, not127.0.0.1. - IPv6 resolution of
localhost:fastcgi_pass localhost:9000can resolve to::1on IPv6-enabled hosts while FPM listens only on127.0.0.1. Use explicit addresses:127.0.0.1:9000. - SELinux: AVC denials can block nginx from connecting to the FPM socket. Check
ausearch -m AVC -ts recentand the audit log.
Prevention
- Run
php-fpm -ttandnginx -tin CI on every config change. Most path mismatches are deploy-time drift that a syntax check would have caught. - Health-check the actual socket nginx dials, not just the systemd unit. A
cgi-fcgiorcurlprobe against the realfastcgi_passtarget catches dead listeners and stale sockets thatsystemctl is-activemisses. - Alert on
listen queuedepth and kernelListenOverflowsbefore they become 502s. By the time nginx logsconnect() failed, the backlog is already full and users are already seeing errors. - Keep the FPM
listendirective and the nginxfastcgi_passin the same config source (templated from one variable) so they cannot drift independently. - Standardize socket placement under
/run/php/to sidestepPrivateTmpand/tmp-namespace issues entirely.
How Netdata helps
- Per-second collection of the PHP-FPM status page (
listen queue,active processes,idle processes,max children reached) catches saturation in the window before the backlog overflows, where 10-second polling misses the buildup entirely. - The nginx collector surfaces upstream 5xx rates per server block, so the 502 burst is visible against normal traffic at the same resolution as the FPM pool metrics.
- Kernel socket counters (
TcpExtListenOverflows,TcpExtListenDrops) and thessview of the FPM listen socket correlate directly with the nginx error log, confirming whether a 502 burst is a dead listener or a saturated one. - Process and systemd collectors flag the master going away within a second, separating a crash from a saturation event without manual
pgrep. For containerized FPM, cgroup memory metrics and OOM-kill events explain a missing master thatsystemctlwould never see.
Related guides
- PHP-FPM 504 Gateway Timeout: requests accepted but never finishing in time
- PHP-FPM active processes near max_children: reading pool utilization
- PHP-FPM in containers: cgroup limits and the silent OOM kill
- PHP-FPM listen queue growing: the earliest signal of saturation
- PHP-FPM “server reached pm.max_children setting (N), consider raising it”
- PHP-FPM crash loop and fork storm: workers dying faster than they serve
- PHP-FPM graceful reload: the brief no-worker window on SIGUSR2






