Back to blog
Linux & Servers Published November 12, 2026

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

Automate WhatsApp Alerts
Start Free ➔
2026/11/12 10:15:32 [error] 1234#1234: *56 connect() failed (111: Connection refused) while connecting to upstream, client: 203.0.113.7, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "example.com"

Visitors see 502 Bad Gateway.

Quick fix: nothing is listening at the address in upstream:. Check it from the Nginx server, start the backend, and try again:

sudo ss -ltnp 'sport = :3000'      # is anything listening on the upstream port?
curl -sv http://127.0.0.1:3000/    # can this server reach it?
sudo systemctl start myapp         # start the backend (use your service name)

Error 111 is the Linux error ECONNREFUSED. Nginx tried to open a TCP connection to the backend, and the backend's machine answered straight away with a reset, which means no process was listening on that IP and port. It is different from 110: Connection timed out, where nothing answered at all. A refusal is fast and clear, so the cause is almost always one of the cases below.


1. Read the upstream address in the log

The end of the log line tells you exactly where Nginx tried to connect:

upstream: "http://127.0.0.1:3000/"

Copy that address and port. Every check below compares it with where your backend really listens.

To find the error lines quickly:

sudo grep "111: Connection refused" /var/log/nginx/error.log | tail -5

2. The backend isn't running

This is the most common cause. The app crashed, failed to start after a reboot, or was stopped during a deploy.

# systemd service
sudo systemctl status myapp
sudo journalctl -u myapp -n 50 --no-pager

# PM2 (Node.js)
pm2 list
pm2 logs myapp --lines 50

# Docker
docker ps -a --format '{{.Names}} {{.Status}}'
docker logs --tail 50 myapp

If the service shows failed or keeps restarting, fix the app error in its logs first. Nginx will work as soon as the backend is listening again. If it was killed with exit code 137, see systemd service keeps restarting: OOM kills and exit code 137.

Make sure the backend starts on boot, so a reboot doesn't bring this error back:

sudo systemctl enable myapp

3. The backend listens on a different port or address

The app is running, but not where Nginx is looking. List what it is really listening on:

sudo ss -ltnp | grep -E 'node|python|gunicorn|uvicorn|php|java'

Compare the output with the upstream: address from the log:

ss showsNginx connects toResult
127.0.0.1:3000127.0.0.1:3000Works
127.0.0.1:8000127.0.0.1:3000111: wrong port
[::1]:3000127.0.0.1:3000111: app listens on IPv6 only
127.0.0.1:3000[::1]:3000111: Nginx tried IPv6

The localhost trap

If your config says proxy_pass http://localhost:3000;, Nginx resolves localhost to both 127.0.0.1 and ::1 and tries them in turn. If the app only listens on one of them, every attempt on the other one logs error 111, even when the site mostly works. Recent Node.js versions can also listen on ::1 only when told to use localhost.

The fix is to use an explicit address on both sides:

location / {
    proxy_pass http://127.0.0.1:3000;
}

And in the app, listen on 127.0.0.1 (or 0.0.0.0 if other machines must reach it) instead of localhost.


4. Nginx and the app run in different Docker containers

Inside a container, 127.0.0.1 means that container only. Two common mistakes:

  • Nginx points to 127.0.0.1:3000, but the app is in another container. Use the service name instead, for example proxy_pass http://app:3000;, with both containers on the same Docker network.
  • The app listens on 127.0.0.1 inside its own container. Other containers can't reach it, and connections are refused. Make the app listen on 0.0.0.0.

Test from inside the Nginx container:

docker exec -it nginx sh -c 'wget -qO- http://app:3000/ || echo FAILED'

If Nginx says host not found in upstream instead of error 111, the problem is the service name, not the port: see Nginx "host not found in upstream" in Docker.


5. PHP-FPM listens on a socket, Nginx uses TCP (or the other way round)

With PHP, check how PHP-FPM is listening:

grep -E '^\s*listen\s*=' /etc/php/*/fpm/pool.d/*.conf
PHP-FPM listen =Nginx fastcgi_pass must be
/run/php/php8.3-fpm.sockunix:/run/php/php8.3-fpm.sock;
127.0.0.1:9000127.0.0.1:9000;

If Nginx uses 127.0.0.1:9000 while PHP-FPM listens on a socket, you get error 111. In the opposite case the error is connect() to unix:/run/php/... failed (2: No such file or directory). After changing either side:

sudo systemctl restart php8.3-fpm
sudo nginx -t && sudo systemctl reload nginx

6. Short bursts of 111 during deploys

If the errors appear only for a few seconds at a time, the backend is being restarted. While the old process has stopped and the new one hasn't started listening yet, every request gets a 502.

Ways to remove that gap:

  • Run two backend instances and list both in an upstream block. Nginx retries the next server when one refuses the connection:

    upstream app {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
    }
    
  • Use a graceful reload if your process manager supports one, for example pm2 reload myapp instead of pm2 restart myapp, or systemctl reload for servers like Gunicorn that handle SIGHUP.

Note that a busy backend usually doesn't refuse connections on Linux. When its queue is full, new connections are dropped and Nginx logs 110: Connection timed out instead. For that case see Nginx 110: Connection timed out while connecting to upstream.


7. Errors that look similar

Log messageMeaningGuide
(111: Connection refused)Nothing listening at that addressThis page
(110: Connection timed out)Nothing answered in time110 guide
(13: Permission denied) while connecting to upstreamNginx isn't allowed to connect (SELinux or socket permissions)Permission denied guide
upstream prematurely closed connectionThe backend accepted, then dropped the connectionPrematurely closed guide

8. Know when it happens on a live server

Error 111 usually starts after a crash, a reboot or a deploy, and the site returns 502 to everyone until someone notices.

With Pingzo, an HTTP monitor calls your site every 1 to 15 minutes depending on your plan and records the status code. A 502 counts as a failed check, an incident opens when 3 of the last 5 checks fail, and alerts go to WhatsApp, Slack, Telegram, Discord or email. See how to monitor an Nginx web server for a step-by-step setup.


Related guides

Zero-Code Uptime Alerts

Stop Finding Out About Outages from Angry Users

Get instant WhatsApp & Discord alerts the second your API, website, or server goes down. Setup in 30 seconds, with checks as often as every minute.

WhatsApp & Discord 60-Second Checks Free Forever Plan
Try Pingzo Free

Know before your users do

Connect official WhatsApp notification channels, Discord webhooks, Telegram bots, and public status pages. Start in 30 seconds.

Create Free Monitor