Back to Blog
Bug Fixes•1 min read

Fix Nginx '502 Bad Gateway' Error

Scaleup Infotech Team
Scaleup Infotech Team

Custom Software & Business Automation

NginxDevOpsDeployment

In this guide

A simple guide to this issue: Nginx '502 Bad Gateway' Error. Learn why it happens, what to check first, and how to fix it.

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.

Relevant Scaleup Infotech Services

Share this article:

Keep Reading

Ready to implement these ideas?

Discuss Your Project