What the message looks like
connect() failed (111: Connection refused) while connecting to upstream, client: 198.51.100.7, server: app.example.com, upstream: "http://127.0.0.1:3000/"Common causes
- The backend service crashed, was stopped or is restarting.
- The backend listens on a different port or a different address only.
proxy_passuseslocalhost, which resolves to ::1, but the app only listens on 127.0.0.1.- Docker container stopped or port not published.
- After a deploy the config still points to an old port.
Check
- Check whether anything listens on the port
ss -ltnp | grep -E ':3000\b' - Call the backend directly without nginx
curl -sv http://127.0.0.1:3000/ -o /dev/null - Show upstream targets in the nginx config
nginx -T 2>/dev/null | grep -nE 'proxy_pass|fastcgi_pass|server .*:[0-9]+' - Show status and recent log lines of the backend service
systemctl status app.service --no-pager -l
Fix
- Start the backend and check the journal for why it stopped.
- In
proxy_passuse the IP the app really listens on, e.g.http://127.0.0.1:3000instead oflocalhost. - Align the port in the app config and nginx, then
nginx -t && systemctl reload nginx. - With Docker publish the port with
-p 127.0.0.1:3000:3000or put nginx on the same network. - Protect the service with
Restart=on-failureso a crash does not cause 502s for hours.
Typical scenario
A Node.js application runs locally on port 3000 and nginx proxies with proxy_pass http://localhost:3000. After an update nginx reports 'connect() failed (111: Connection refused) while connecting to upstream', specifically to [::1]:3000. The service listens on IPv4 only, but for the name localhost nginx also tries IPv6. Use ss -ltnp | grep 3000 to see on which address the application really listens, and change proxy_pass to 127.0.0.1.
FAQ
How is this different from “upstream timed out (110)”?
111 means nothing is listening and the connection is refused at once. 110 means the backend listens but does not answer in time – that gives a 504.
Why does it only happen right after a deploy?
The service needs a few seconds to start, and connections are refused meanwhile. Fix: a health check before switching, or several instances.
How does 111 differ from 110 and 113?
111 means the target host actively refuses the connection at once, so no process listens on the port. 110 is a timeout, for example from a firewall dropping packets. 113 is 'No route to host'. The number narrows down the search considerably.
Why does the error appear after a reboot?
At boot nginx may start before the backend. The upstream is then not ready yet and nginx reports 111. A suitable After= ordering in the systemd unit, or a healthcheck in the container, solves this cleanly.
Can a container port mapping be the cause?
Yes. If the application inside the container listens only on 127.0.0.1, it cannot be reached from outside even though the port is published. The process has to listen on 0.0.0.0 inside the container for the mapping to work.