What the message looks like
2026/10/23 09:14:02 [error] 1234#1234: *5678 connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 198.51.100.7, server: cloud.beispiel.de, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"Common causes
- PHP-FPM is stopped or crashed (e.g. after an update or an OOM kill).
- The socket path no longer matches after a PHP upgrade, e.g.
php8.2-fpm.sockinstead ofphp8.3-fpm.sock. - No permission on the socket:
listen.owner/listen.groupdo not match the nginx user. - The backend listens on a different port or only on IPv6/IPv4.
- The PHP worker dies mid-request (segfault):
upstream prematurely closed connection.
Check
- Read the latest errors in the nginx log
tail -n 50 /var/log/nginx/error.log - Check PHP-FPM status
systemctl status php8.3-fpm - Does the socket exist and who owns it?
ls -l /run/php/ - PHP-FPM logs, including crashes
- Is a TCP backend listening on the expected port?
ss -ltnp | grep -E ':(9000|3000|8000) '
Fix
- Restart PHP-FPM or the backend with
systemctl restart php8.3-fpm, then look for the root cause in the journal. - Point
fastcgi_passto the correct socket, thennginx -t && systemctl reload nginx. - Set
listen.owner = www-dataandlisten.group = www-datain the FPM pool. - On OOM kills, size
pm.max_childrento the available RAM. - Protect the backend with systemd
Restart=on-failure.
Typical scenario
After an apt upgrade on the web server every PHP page returns '502 Bad Gateway' while static files still load. The nginx error log says 'connect() to unix:/run/php/php8.2-fpm.sock failed (2: No such file or directory)', although PHP 8.3 is now running. First use ls /run/php/ to see which socket file actually exists and compare it with fastcgi_pass in the active site configuration.
FAQ
What is the difference between 502 and 504?
502: the backend does not answer at all or answers garbage. 504: the backend is reachable but takes longer than the nginx timeout.
Why does 502 only happen occasionally?
Typical for crashing workers or a full listen backlog. Look for SIGSEGV or OOM messages in the journal and Resource temporarily unavailable in the nginx log.
What is the difference between 502 and 504?
With 502, nginx gets no usable answer from the backend because it is unreachable or the connection breaks. With 504, the backend is reachable but does not answer in time. The error log names the exact reason each time.
Why does the 502 only appear under load?
Often all PHP-FPM workers are busy or were killed. The FPM log then shows 'server reached pm.max_children setting'. More workers only help if RAM allows it; otherwise you just hand the problem to the OOM killer.
Is restarting PHP-FPM a real fix?
It often removes the outage for the moment but not the cause. Afterwards check the FPM log and dmesg for crashes or OOM kills, otherwise the 502 returns with the next load spike.