Tools: Latest: How to Fix 502 Bad Gateway in Nginx (With Exact Commands)

Tools: Latest: How to Fix 502 Bad Gateway in Nginx (With Exact Commands)

How to Fix 502 Bad Gateway in Nginx (With Exact Commands)

What Does 502 Bad Gateway Actually Mean?

Step 1: Check If Nginx Is Actually Running

Step 2: Check Your Upstream App

Step 3: Read the Error Logs

Step 4: Restart Your App Process

Step 5: Fix Common nginx.conf Issues

Step 6: If App Keeps Crashing

Quick Decision Tree

Most Common Root Causes (from real production issues) 502 Bad Gateway is one of the most panic-inducing errors in production. Your site is down, users are getting errors, and you have no idea where to start. This guide gives you the exact diagnostic steps and fixes — no placeholder commands. Nginx received a bad response (or no response) from the upstream server. Nginx itself is fine. Something behind Nginx is broken. If it's active, Nginx is fine. Move to step 2. Replace 3000 with your app's port. If you get Connection refused, your app is down — not Nginx. Look for: connect() failed, upstream timed out, no live upstreams. Debugging production errors at 3AM shouldn't take hours. Step2Dev — paste your error, get the exact fix for your infra in 60 seconds. No generic answers. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to ? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 500;">systemctl -weight: 500;">status nginx -weight: 500;">systemctl -weight: 500;">status nginx -weight: 500;">systemctl -weight: 500;">status nginx nginx -t -weight: 500;">systemctl -weight: 500;">restart nginx nginx -t -weight: 500;">systemctl -weight: 500;">restart nginx nginx -t -weight: 500;">systemctl -weight: 500;">restart nginx -weight: 500;">curl -I http://127.0.0.1:3000 -weight: 500;">curl -I http://127.0.0.1:3000 -weight: 500;">curl -I http://127.0.0.1:3000 Connection refused tail -100 /var/log/nginx/error.log tail -100 /var/log/nginx/error.log tail -100 /var/log/nginx/error.log connect() failed upstream timed out no live upstreams # PM2 pm2 -weight: 500;">restart all pm2 logs --lines 50 # Systemd -weight: 500;">systemctl -weight: 500;">restart myapp journalctl -u myapp -n 50 --no-pager # Docker -weight: 500;">docker ps -a -weight: 500;">docker -weight: 500;">restart my-container -weight: 500;">docker logs my-container --tail 50 # PM2 pm2 -weight: 500;">restart all pm2 logs --lines 50 # Systemd -weight: 500;">systemctl -weight: 500;">restart myapp journalctl -u myapp -n 50 --no-pager # Docker -weight: 500;">docker ps -a -weight: 500;">docker -weight: 500;">restart my-container -weight: 500;">docker logs my-container --tail 50 # PM2 pm2 -weight: 500;">restart all pm2 logs --lines 50 # Systemd -weight: 500;">systemctl -weight: 500;">restart myapp journalctl -u myapp -n 50 --no-pager # Docker -weight: 500;">docker ps -a -weight: 500;">docker -weight: 500;">restart my-container -weight: 500;">docker logs my-container --tail 50 upstream backend { server 127.0.0.1:3000; # Use IP, not 'localhost' keepalive 32; } server { location / { proxy_pass http://backend; proxy_read_timeout 90s; # Add this if timing out proxy_connect_timeout 10s; } } upstream backend { server 127.0.0.1:3000; # Use IP, not 'localhost' keepalive 32; } server { location / { proxy_pass http://backend; proxy_read_timeout 90s; # Add this if timing out proxy_connect_timeout 10s; } } upstream backend { server 127.0.0.1:3000; # Use IP, not 'localhost' keepalive 32; } server { location / { proxy_pass http://backend; proxy_read_timeout 90s; # Add this if timing out proxy_connect_timeout 10s; } } nginx -t && -weight: 500;">systemctl reload nginx nginx -t && -weight: 500;">systemctl reload nginx nginx -t && -weight: 500;">systemctl reload nginx # Check memory free -h # Check disk space (full disk = crash) df -h # Check CPU top -bn1 | head -20 # Check memory free -h # Check disk space (full disk = crash) df -h # Check CPU top -bn1 | head -20 # Check memory free -h # Check disk space (full disk = crash) df -h # Check CPU top -bn1 | head -20 502 Error ├── -weight: 500;">curl 127.0.0.1:PORT → Connection refused? │ └── App is down → Check app logs → Restart app ├── -weight: 500;">curl works fine? │ └── Nginx config issue → Check proxy_pass → nginx -t └── Intermittent 502? └── App overloaded → Add proxy_read_timeout → Scale up 502 Error ├── -weight: 500;">curl 127.0.0.1:PORT → Connection refused? │ └── App is down → Check app logs → Restart app ├── -weight: 500;">curl works fine? │ └── Nginx config issue → Check proxy_pass → nginx -t └── Intermittent 502? └── App overloaded → Add proxy_read_timeout → Scale up 502 Error ├── -weight: 500;">curl 127.0.0.1:PORT → Connection refused? │ └── App is down → Check app logs → Restart app ├── -weight: 500;">curl works fine? │ └── Nginx config issue → Check proxy_pass → nginx -t └── Intermittent 502? └── App overloaded → Add proxy_read_timeout → Scale up process.on('unhandledRejection') pm2 -weight: 500;">restart --max-memory--weight: 500;">restart 512M - Your Node.js/Python/PHP app has crashed - Wrong upstream port configured - App is binding to localhost but Nginx is trying 127.0.0.1 (or vice versa) - App is overloaded and not responding in time - App crashed due to unhandled promise rejection — add process.on('unhandledRejection') - Port mismatch — app on 3001 but Nginx pointing to 3000 - App bound to 0.0.0.0 but Nginx using localhost — use 127.0.0.1 everywhere - Memory exhaustion — app OOM killed, use pm2 -weight: 500;">restart --max-memory--weight: 500;">restart 512M