Deployment rescue guide

Fix Nginx 502 Bad Gateway without guessing

A 502 means Nginx is reachable but cannot get a valid response from your app. The fix is usually in the upstream process, port, socket, or proxy config.

What is probably broken

The domain opens, Nginx responds, but every request ends in 502 while the app seems fine locally.

What production should look like

Nginx proxies to a healthy app process, logs show the real failure, and the domain returns the app instead of a gateway error.

STEP 01

Check whether the app is alive

Do not start with Nginx config. First confirm the upstream app is running and listening where Nginx expects it.

  • Check process status in PM2, systemd, Docker, or your supervisor.
  • Curl the app from the VPS using localhost and the expected port.
  • Read app logs for crashes caused by missing env vars or build files.

STEP 02

Verify the proxy target

The most common 502 is a mismatch between Nginx `proxy_pass` and the actual app port or socket.

  • Confirm the app port matches `proxy_pass` exactly.
  • Avoid proxying to `0.0.0.0`; use `127.0.0.1` or a socket.
  • Reload Nginx only after `nginx -t` passes.

STEP 03

Find the real error in logs

Nginx error logs usually tell you whether the upstream refused, timed out, or returned invalid headers.

  • Use `/var/log/nginx/error.log` for proxy errors.
  • Use app logs for runtime crashes and env failures.
  • Check firewall rules only after local curl fails.

Quick check

Is your app production-ready?

If you cannot tick all five, we can fix it. Send us the repo and we handle the rest.

  • ?Custom domain with HTTPS
  • ?Environment variables configured
  • ?Database wired and backed up
  • ?Auto-deploy on push
  • ?Smoke tested and live

Free checklist

Deploying this yourself?

Get the exact 5-step checklist we run before every deploy. No fluff, just the things that actually break.

No spam. Your email is only used to send the checklist.

Done-for-you deploy

If the 502 is blocking launch, RepoAssistant can diagnose the server, fix the upstream, and ship the app.

Fixed price per repo. 24h delivery or full refund. No hourly billing.

Deploy my app

Related guides