Fix "connect() failed (111: Connection refused) while connecting to upstream" (Nginx 502)
2026/11/27 09:14:02 [error] 31#31: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8080"
- connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: example.com, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:3000/api"
- connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory) while connecting to upstream
- connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream
- [emerg] 1#1: host not found in upstream "app:9000" in /etc/nginx/conf.d/default.conf:14
- 502 Bad Gateway
Nginx returns 502 Bad Gateway because nothing listens at its upstream address: PHP-FPM, Node or Gunicorn is stopped, listens on another port or socket, or, in Docker, 127.0.0.1 means the Nginx container itself. Check that the backend runs, then align fastcgi_pass or proxy_pass with its real address (the Compose service name in Docker).
What this error means
As a reverse proxy, Nginx opens a connection to the backend for every request: TCP for proxy_pass http://... or fastcgi_pass 127.0.0.1:9000, a Unix socket for unix:/run/php/...sock. Code 111 is ECONNREFUSED: the target machine answers, but no process listens on that port, and the kernel rejects the connection. Nginx then writes this line to error.log and returns 502 to the browser. The upstream: field of the log gives the exact address it tried. Close cousins: (2: No such file or directory) when the Unix socket does not exist, (13: Permission denied) when Nginx is not allowed to open it.
Common causes
- Backend stopped or crashed: PHP-FPM is not started, the Node process crashed, Gunicorn failed to load the application.
- Wrong port or wrong socket: Nginx targets
127.0.0.1:9000while PHP-FPM listens on/run/php/php8.3-fpm.sock(Debian and Ubuntu default), or the socket was renamed after a PHP upgrade. - Docker:
localhostinstead of the service name: inside the Nginx container,127.0.0.1is the Nginx container. You must target the service (app:9000), and the backend must listen on0.0.0.0, not on its own container’s127.0.0.1. - Backend not ready yet or recreated: Nginx starts before the application, or the backend container was recreated with a new IP that Nginx has not re-resolved.
- SELinux (RHEL, Rocky, Fedora): the policy forbids Nginx from opening network connections, which usually gives
(13: Permission denied).
Step-by-step fix
-
Read the log and spot the upstream
Note the
upstream:value: it tells which protocol, address and port Nginx tried. That is the address to check, not the browser URL.sudo tail -n 30 /var/log/nginx/error.log docker compose logs --tail=30 nginx -
Check that the backend really listens
On the Linux host,
sslists open ports and the process behind them. If nothing listens, start the service and read its own log: the real error (crash on start, invalid config) is there.sudo ss -ltnp | grep -E ':(9000|3000|8000)\b' ls -l /run/php/ sudo systemctl status php8.3-fpm --no-pager sudo systemctl start php8.3-fpm -
Align Nginx with the PHP-FPM address
Read the pool’s
listendirective (grep -E '^listen' /etc/php/8.3/fpm/pool.d/www.conf) and use exactly the same value infastcgi_pass. Test the config withsudo nginx -t, then reload withsudo systemctl reload nginx.location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; } -
Node or Gunicorn: same port on both sides
The
proxy_passport must be the one the application really listens on. Test the backend without Nginx usingcurl: if it fails too, the problem is in the application, not in Nginx.gunicorn --bind 127.0.0.1:8000 myproject.wsgi:application curl -I http://127.0.0.1:8000 curl -I http://127.0.0.1:3000 -
Docker Compose: target the service name
Replace
127.0.0.1with the Compose service name: Docker resolves it on the shared network. On the backend side, listen on0.0.0.0(app.listen(3000, '0.0.0.0'),gunicorn --bind 0.0.0.0:8000). The officialphp:8.3-fpmimage already listens on port 9000 on all interfaces.location ~ \.php$ { fastcgi_pass app:9000; fastcgi_param SCRIPT_FILENAME /var/www/html/public$fastcgi_script_name; include fastcgi_params; } location /api/ { proxy_pass http://web:3000; } -
Handle startup order and changing IPs
Nginx resolves names at startup. After recreating the backend, run
docker compose restart nginx, or let Nginx resolve on every request through Docker’s internal DNS. Also add ahealthcheckto the backend anddepends_onwithcondition: service_healthy.resolver 127.0.0.11 valid=10s; location /api/ { set $backend http://web:3000; proxy_pass $backend; }
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Supervise the backend so it restarts on its own:
Restart=on-failurein systemd,restart: unless-stoppedin Compose, or PM2 for Node. - After a PHP upgrade (8.2 to 8.3, for example), update the socket path in Nginx and stop the old FPM service.
- Under SELinux, do not turn SELinux off: only allow what Nginx needs with
sudo setsebool -P httpd_can_network_connect 1.
Frequently asked questions
What is the difference between a 502 and a 504 error with Nginx?
502 Bad Gateway: the connection to the backend was refused or its response was invalid. 504 Gateway Timeout: the backend accepted the connection but did not answer in time (upstream timed out (110: Connection timed out)).
Why does curl localhost:3000 work on the host but not from Nginx in Docker?
Each container has its own localhost. From the Nginx container, target the service name (web:3000); to reach an application running on the host, use host.docker.internal (Docker Desktop, or extra_hosts: ["host.docker.internal:host-gateway"] on Linux).
Is Nginx the problem?
Rarely: Nginx works and is precisely reporting that the backend does not answer. Fix the backend or the target address; restarting Nginx alone only helps when a container IP has changed.
Useful tools for this error
More Docker & DevOps errors
- Fix "permission denied while trying to connect to the Docker daemon socket"
- Fix "Bind for 0.0.0.0:8080 failed: port is already allocated" (Docker)
- Fix "no space left on device" (Docker and Linux)
- Fix "exec format error" in Docker (arm64 / amd64)
- Fix "Cannot connect to the Docker daemon. Is the docker daemon running?"
- Fix "exited with code 137" (OOMKilled) in Docker and Kubernetes
- Fix "CrashLoopBackOff" / "Back-off restarting failed container" (Kubernetes)