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 showFix it fast
Most common cause, 1 of 3The 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.
# 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;
}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.
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.
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:
- 1The pool listens on a Unix socket, not on a TCP port
ss lists a .sock file owned by php-fpm and nothing on :9000, and the pool's listen line holds a path - 2nginx and PHP-FPM run in separate containers
Docker Compose runs nginx and a php:*-fpm image as two services, and fastcgi_pass says 127.0.0.1:9000 or localhost:9000 - 3FPM is bound to loopback while nginx connects from another host
ss 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 upstreamupstream: "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.
grep -R "^listen" /etc/php/*/fpm/pool.d/ /etc/php-fpm.d/ 2>/dev/nullFix
Point nginx at the socket with the unix: prefix, using the path from the listen line. Keep port 9000 closed.
# 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.
sudo nginx -t && sudo systemctl reload nginx
curl -I http://localhost/index.phpConfirmed by php.net
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.
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.
# 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.
docker compose up -d --force-recreate
curl -I http://localhost/index.phpConfirmed by docs.docker.com
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.
sudo ss -ltnp | grep 9000Fix
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.
; /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-fpmVerify
ss now shows php-fpm on 10.0.0.5:9000, and a PHP page requested through the nginx host answers.
sudo ss -ltnp | grep 10.0.0.5:9000Confirmed by php.net
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.