Skip to content
FR
  1. LuzineDebug
  2. Docker & DevOps
  3. 2026/11/27 09:14:02 [error] 31#31: *7 connect()…
Docker & DevOps Intermediate ≈ 15 min to fix

Fix "connect() failed (111: Connection refused) while connecting to upstream" (Nginx 502)

Error message
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
In short

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).

Be the first to confirm this fix

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

  1. Backend stopped or crashed: PHP-FPM is not started, the Node process crashed, Gunicorn failed to load the application.
  2. Wrong port or wrong socket: Nginx targets 127.0.0.1:9000 while PHP-FPM listens on /run/php/php8.3-fpm.sock (Debian and Ubuntu default), or the socket was renamed after a PHP upgrade.
  3. Docker: localhost instead of the service name: inside the Nginx container, 127.0.0.1 is the Nginx container. You must target the service (app:9000), and the backend must listen on 0.0.0.0, not on its own container’s 127.0.0.1.
  4. 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.
  5. SELinux (RHEL, Rocky, Fedora): the policy forbids Nginx from opening network connections, which usually gives (13: Permission denied).

Step-by-step fix

  1. 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.

    bash
    sudo tail -n 30 /var/log/nginx/error.log
    docker compose logs --tail=30 nginx
  2. Check that the backend really listens

    On the Linux host, ss lists 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.

    bash
    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
  3. Align Nginx with the PHP-FPM address

    Read the pool’s listen directive (grep -E '^listen' /etc/php/8.3/fpm/pool.d/www.conf) and use exactly the same value in fastcgi_pass. Test the config with sudo nginx -t, then reload with sudo systemctl reload nginx.

    nginx
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
  4. Node or Gunicorn: same port on both sides

    The proxy_pass port must be the one the application really listens on. Test the backend without Nginx using curl: if it fails too, the problem is in the application, not in Nginx.

    bash
    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
  5. Docker Compose: target the service name

    Replace 127.0.0.1 with the Compose service name: Docker resolves it on the shared network. On the backend side, listen on 0.0.0.0 (app.listen(3000, '0.0.0.0'), gunicorn --bind 0.0.0.0:8000). The official php:8.3-fpm image already listens on port 9000 on all interfaces.

    nginx
    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;
    }
  6. 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 a healthcheck to the backend and depends_on with condition: service_healthy.

    nginx
    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-failure in systemd, restart: unless-stopped in 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

All Docker & DevOps errors →