Port & Network Error

Fix "502 Bad Gateway" in Nginx & Reverse Proxies

Developer guide and instant terminal command to diagnose and solve this error.

Terminal Output
$502 Bad Gateway (connect() failed (111: Connection refused) while connecting to upstream)

Why Does This Error Happen?

Nginx received an invalid or nonexistent response from the upstream application server (Node.js, Gunicorn, PHP-FPM) because the backend service is stopped or listening on a different port.

Instant Terminal One-Liner Fixes

Linux (Bash / Zsh)
sudo systemctl status my-app && sudo nginx -t && sudo systemctl reload nginx
macOS (Terminal / iTerm2)
brew services restart nginx
Windows (PowerShell Run as Administrator)
nginx.exe -s reload

Comprehensive Step-by-Step Resolution

  1. 1Verify backend application status: "systemctl status my-app" or "pm2 list".
  2. 2Check upstream port in /etc/nginx/sites-available/: Ensure proxy_pass matches the active local port (e.g. http://127.0.0.1:3000).
  3. 3Inspect Nginx error logs: "sudo tail -f /var/log/nginx/error.log".
Dedicated Companion Tool

Troubleshoot using the Nginx Reverse Proxy Config Generator

Use our client-side utility with zero server upload for instant debugging.

Open Tool

Frequently Asked Questions

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

Nginx received an invalid or nonexistent response from the upstream application server (Node.js, Gunicorn, PHP-FPM) because the backend service is stopped or listening on a different port.