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 shows | Nginx connects to | Result |
|---|---|---|
127.0.0.1:3000 | 127.0.0.1:3000 | Works |
127.0.0.1:8000 | 127.0.0.1:3000 | 111: wrong port |
[::1]:3000 | 127.0.0.1:3000 | 111: app listens on IPv6 only |
127.0.0.1:3000 | [::1]:3000 | 111: 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 exampleproxy_pass http://app:3000;, with both containers on the same Docker network. - The app listens on
127.0.0.1inside its own container. Other containers can't reach it, and connections are refused. Make the app listen on0.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.sock | unix:/run/php/php8.3-fpm.sock; |
127.0.0.1:9000 | 127.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
upstreamblock. 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 myappinstead ofpm2 restart myapp, orsystemctl reloadfor servers like Gunicorn that handleSIGHUP.
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 message | Meaning | Guide |
|---|---|---|
(111: Connection refused) | Nothing listening at that address | This page |
(110: Connection timed out) | Nothing answered in time | 110 guide |
(13: Permission denied) while connecting to upstream | Nginx isn't allowed to connect (SELinux or socket permissions) | Permission denied guide |
upstream prematurely closed connection | The backend accepted, then dropped the connection | Prematurely 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
- 502 Bad Gateway during traffic spikes
- Nginx "upstream prematurely closed connection": how to fix
- Fix 504 Gateway Timeout with Nginx and PHP-FPM
- Fix: nginx "upstream sent too big header" (502 Bad Gateway)
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.