Status codes · one code502Bad Gateway

What a 502 Bad Gateway response means, who is at fault, what a visitor should do and what the server owner should do.

Open the reference on this code
What is happening

The proxy could not reach the application

connect() failed (111: Connection refused) while connecting to upstream

nginx accepted the request and tried to pass it on — to an application, a container, another server — and got no connection. Connection refused means nobody is listening at that address: the process is not running, has crashed, or is listening on a different port or socket.

Code
Who is at faultServer error
Class5xx Server error
01

What to do

If it is someone else's site

There is nothing to do from outside: the site is up but its inner part is unreachable. That usually lasts minutes — the time a restart takes — so retrying a little later makes sense.

If it is your server

Check three things in order: whether the application process is alive, which address it listens on, and whether that address matches proxy_pass. The classic cases: the app crashed after a deploy; the port was still held by the previous process and the new one came up next door; in Docker the service is reachable by container name while the config says 127.0.0.1. If the process is alive and the port is right yet the 502 persists, look at 504 instead: that one is about timeouts.

02

Easily confused with

504Gateway Timeout

the connection was made but no answer arrived: the upstream is alive and slow.

504
500Internal Server Error

the application did answer — with an error. So it is running, and the problem is in the code.

500
503Service Unavailable

the upstream is absent on purpose: maintenance or a node taken out by the balancer.

503
03

How to check

ss -lptn 'sport = :3000'

whether anything listens on the port proxy_pass aims at

curl -sI http://127.0.0.1:3000/ | head -1

whether the upstream answers directly, bypassing the proxy

Find another code

The reference holds sixty-four codes searchable by number, name and meaning — plus a breakdown of confusing pairs such as 401 and 403.

Open the reference on this codeAll codes

Updated

«» added to favorites