Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

nginx is refused on 127.0.0.1:9000 because PHP-FPM listens somewhere else

Your error

connect() to 127.0.0.1:9000 failed (111: Connection refused) while connecting to upstream2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The pool listens on a Unix socket, not on a TCP port

Point nginx at the socket with the unix: prefix, using the path from the listen line.

Fix
# nginx site config, path copied from the pool's listen line
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

When nginx logs connect() to 127.0.0.1:9000 failed (111: Connection refused) and ss shows no listener there, PHP-FPM uses another address. A path in the pool's listen line means a Unix socket, so point fastcgi_pass at it. In Docker, use the PHP service name; across hosts, bind FPM to the private address and set listen.allowed_clients. Applies to PHP-FPM 8.2-8.6.

  • Applies to PHP-FPM 8.2-8.6, nginx 1.30 stable and 1.31 mainline, Docker official php *-fpm images
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List what PHP-FPM really listens on, next to the pool's listen line and nginx's fastcgi_pass. A socket path and no port is cause 1. nginx and PHP in separate containers is cause 2. A listener on 127.0.0.1:9000 while nginx runs on another machine is cause 3. No php-fpm process in the list at all means the service is down.

Run this first
sudo ss -ltnpx | grep -E "php-fpm|:9000"
grep -R "^listen" /etc/php/*/fpm/pool.d/ /etc/php-fpm.d/ 2>/dev/null
grep -R "fastcgi_pass" /etc/nginx/

Then find the line you see in this list and open its fix:

  1. 1The pool listens on a Unix socket, not on a TCP portss lists a .sock file owned by php-fpm and nothing on :9000, and the pool's listen line holds a path
  2. 2nginx and PHP-FPM run in separate containersDocker Compose runs nginx and a php:*-fpm image as two services, and fastcgi_pass says 127.0.0.1:9000 or localhost:9000
  3. 3FPM is bound to loopback while nginx connects from another hostss shows 127.0.0.1:9000, but nginx runs on a different machine or VM and connects to this server's private IP

What does the error look like?

  • connect() to 127.0.0.1:9000 failed (111: Connection refused) while connecting to upstream
  • upstream: "fastcgi://127.0.0.1:9000"
  • 502 Bad Gateway

Cause 1 of 3

The pool listens on a Unix socket, not on a TCP port

You seess lists a .sock file owned by php-fpm and nothing on :9000, and the pool's listen line holds a path

Check

Connection refused on 127.0.0.1:9000 with a socket path in the pool file means FPM never opened that port. The PHP manual accepts ip:port, a lone port number or a socket file path in listen, and recommends the socket when the web server shares the host. nginx then has to name the same socket.

Check
grep -R "^listen" /etc/php/*/fpm/pool.d/ /etc/php-fpm.d/ 2>/dev/null

Fix

Point nginx at the socket with the unix: prefix, using the path from the listen line. Keep port 9000 closed.

Fix
# nginx site config, path copied from the pool's listen line
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}

Verify

Test and reload nginx, then request a PHP page: it returns 200 and the error log stays quiet.

Verify
sudo nginx -t && sudo systemctl reload nginx
curl -I http://localhost/index.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

nginx and PHP-FPM run in separate containers

You seeDocker Compose runs nginx and a php:*-fpm image as two services, and fastcgi_pass says 127.0.0.1:9000 or localhost:9000

Check

In a container setup the refusal comes from nginx looking at itself. The official php FPM images write listen = 9000 into php-fpm.d/docker.conf, but that port belongs to the PHP container; Docker's networking docs note that 127.0.0.1 refers to a container's own loopback address. Compose makes each service reachable by its service name on the shared network.

Check
docker compose exec php sh -c 'grep -R "^listen" /usr/local/etc/php-fpm.d/'

Fix

Use the PHP service name in fastcgi_pass, and do not publish port 9000 on the host; the PHP manual warns that an exposed FastCGI endpoint allows code execution.

Fix
# compose.yaml: no ports: entry for php
services:
  php:
    image: php:8.5-fpm
  nginx:
    image: nginx:1.30
    ports: ["80:80"]

# nginx config inside the nginx container
fastcgi_pass php:9000;

Verify

Recreate the containers and request a PHP page through nginx.

Verify
docker compose up -d --force-recreate
curl -I http://localhost/index.php

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

FPM is bound to loopback while nginx connects from another host

You seess shows 127.0.0.1:9000, but nginx runs on a different machine or VM and connects to this server's private IP

Check

A pool with listen = 127.0.0.1:9000 is bound to the loopback address, which no other machine can reach, so a web server elsewhere is refused at the private IP. Opening it up has a cost: the PHP manual says that when a TCP socket is used, the allowed clients must be listed in listen.allowed_clients, which otherwise accepts any address.

Check
sudo ss -ltnp | grep 9000

Fix

Bind the pool to the private interface only, list the nginx host as the one allowed client, and keep the port closed to the internet in the firewall.

Fix
; /etc/php/8.3/fpm/pool.d/www.conf
listen = 10.0.0.5:9000
listen.allowed_clients = 10.0.0.4

# Ubuntu 24.04 service name
sudo systemctl restart php8.3-fpm

Verify

ss now shows php-fpm on 10.0.0.5:9000, and a PHP page requested through the nginx host answers.

Verify
sudo ss -ltnp | grep 10.0.0.5:9000

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Pick one transport per setup and write it down: a Unix socket on a single host, a service name in Compose, a private address with allowed clients across hosts.
  • Keep the fastcgi_pass value in one nginx snippet that every PHP site includes.
  • After a PHP upgrade or a new pool file, compare ss output with fastcgi_pass before reloading nginx.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Dispatch Pro, stegnor.com.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.