Scaleup Infotech
Scaleup Infotech.
Back to Blog
Bug Fixes8 min read

Fix Nginx '502 Bad Gateway' Error

Scaleup Infotech

Scaleup Infotech

Software & Marketing Agency

Apr 25, 2026
Fix Nginx '502 Bad Gateway' Error
NginxDevOpsDeployment

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

bash
sudo tail -f /var/log/nginx/error.log

connect() failed (111: Connection refused) means your app is down. upstream timed out means it is too slow.

Cause 1: The App Process Crashed

bash
pm2 status          # Node
pm2 restart app
sudo systemctl status php8.3-fpm   # PHP-FPM

Cause 2: Wrong upstream Address or Socket

nginx
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.

Share this article:

Keep Reading

Ready to implement these ideas?

Work With Scaleup Infotech