Solution:
Root Cause: Dead or Uncreated UNIX Domain Socket
Nginx returns
502 Bad Gateway with error
(2: No such file or directory) when the
fastcgi_pass or
proxy_pass directive targets a UNIX socket path (
unix:/run/php/php-fpm.sock) that does not exist on the filesystem. This occurs when the upstream service (PHP-FPM, Gunicorn, Puma) failed to start, crashed during deployment initialization, or created the socket file in a different directory path than Nginx expects.
Diagnostic Verification:
1.Check Nginx log output:
bash
tail -n 25 /var/log/nginx/error.log
2.Verify whether the socket file physically exists on disk:
bash
ls -la /run/php/ /var/run/
3.Query the operational status of the upstream daemon:
bash
systemctl status php8.3-fpm # or gunicorn, uwsgi
Step-by-Step Fix:
1.Update Upstream Pool Configuration:
Open your application pool configuration file (e.g., /etc/php/8.3/fpm/pool.d/www.conf or Gunicorn systemd unit).Confirm the listen property matches the exact path defined in Nginx: ini
listen = /run/php/php8.3-fpm.sock
2.Restart Upstream Application Daemon:
Execute: bash
systemctl restart php8.3-fpm
3.Verify Socket File Creation:
Confirm the socket file is active: bash
test -S /run/php/php8.3-fpm.sock && echo "Socket Active"
4.Test Nginx Configuration and Reload:
Test syntax and reload Nginx: bash
nginx -t && systemctl reload nginx
Prevention & Long-Term Monitoring:
Implement Systemd service restart directives (Restart=always and RestartSec=3s) inside upstream service unit files to recover automatically from deployment initialization crashes.