How to Fix an Nginx 502 Bad Gateway

A 502 Bad Gateway from nginx means nginx is up, but the backend it proxies to did not answer. Nine times out of ten it's a wrong upstream address/port or a backend that isn't listening. Here's how to pin it down and fix it.

DevOps Engineernginxlinuxreverse-proxy

What a 502 Bad Gateway actually means

Nginx returns 502 when it is acting as a reverse proxy and the upstream server (your app) either refused the connection, wasn't listening where nginx expected, or returned a malformed response. The nginx process itself is healthy - the problem is between nginx and your backend.

Step 1: confirm it and read the error log

curl -i http://localhost/        # confirms the 502
tail -n 20 /var/log/nginx/error.log

The error log names the cause directly. The most common line is:

connect() failed (111: Connection refused) while connecting to upstream,
upstream: "http://127.0.0.1:8080/"

Connection refused to a specific upstream: is the smoking gun - nginx is dialing a port nothing is listening on.

Step 2: check where the app is really listening

ss -ltnp | grep LISTEN     # what ports are actually open

Compare that to the proxy_pass / upstream in your nginx config:

upstream backend {
    server 127.0.0.1:8080;   # <- nginx dials 8080
}

If the app is on 3000 but nginx points at 8080, that mismatch is your 502.

Step 3: fix the upstream and reload

Point nginx at the real port and reload (no full restart needed):

# in /etc/nginx/nginx.conf, set the upstream to the real port, then:
nginx -t            # validate the config first
nginx -s reload     # graceful reload

curl http://localhost/ should now return 200.

The other common 502 causes

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

What is an Nginx 502 Bad Gateway?

A 502 Bad Gateway means nginx is working but the upstream server it proxies to (your app) returned no valid response - because the app refused the connection, isn't listening where nginx expects, or replied with garbage. Nginx itself is healthy; the problem is between nginx and your backend.

How do I fix a 502 Bad Gateway in nginx?

Read /var/log/nginx/error.log to find the upstream nginx tried and why it failed (e.g. Connection refused), confirm where your app is actually listening with ss -ltnp, point nginx's proxy_pass/upstream at the real address, then nginx -t and nginx -s reload.

Does a 502 Bad Gateway mean I'm blocked?

No. A 502 is a server-side error between nginx and its upstream, not a sign you are blocked or banned. Being blocked usually returns 403 (Forbidden) or 429 (Too Many Requests); a 502 means the backend didn't answer.

Do I need to restart nginx after fixing a 502?

No - run nginx -t to validate the config, then nginx -s reload for a graceful reload that keeps existing connections alive. A full restart is rarely needed.

Keep learning

Recover a Crashed Linux ServiceDevOps projectCorrect a Postgres Port MismatchDevOps projectReopen the Redis Bind Address (Connection Refused)DevOps projectDevOps roadmapStep by step to hiredDevOps interview questionsSTAR answersAll DevOps projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →