A 502 from Nginx is rarely Nginx's fault — it is the messenger. It means the upstream service Nginx proxies to (your Node app, PHP-FPM, Gunicorn) did not return a valid response. Start with the logs.
The Error
502 Bad Gateway — nginx
Step 1: Read the Error Log
sudo tail -f /var/log/nginx/error.logconnect() failed (111: Connection refused) means your app is down. upstream timed out means it is too slow.
Cause 1: The App Process Crashed
pm2 status # Node
pm2 restart app
sudo systemctl status php8.3-fpm # PHP-FPMCause 2: Wrong upstream Address or Socket
location / {
proxy_pass http://127.0.0.1:3000; # must match your app's port
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_read_timeout 60s; # raise for slow endpoints
}Always Test Before Reloading
Run sudo nginx -t to validate the config, then sudo systemctl reload nginx. Reloading a broken config can take the whole site down.
