nginx: connect() failed (111: Connection refused) while connecting to upstream

nginx: connect() failed (111: Connection refused) while connecting to upstream

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

nginx: connect() failed (111: Connection refused) while connecting to upstream

When your browser returns a 502 Bad Gateway error, the immediate instinct is often to check the web server logs. If you see the specific message connect() failed (111: Connection refused), the issue isn't with the network reachability in the traditional sense; it's about process availability. The operating system explicitly told Nginx that the target address is unreachable because no process is listening there.

In the scenario provided, Nginx attempted to establish a FastCGI connection to 127.0.0.1:9000. This is the standard port for PHP-FPM (PHP FastCGI Process Manager). The error code 111 corresponds to ECONNREFUSED, which means the local loopback interface was up, but nothing accepted the incoming connection on that specific port.

The most common culprit is a mismatch between the Nginx configuration and the actual state of the PHP process manager. You might have started the web server but failed to start PHP-FPM, or perhaps you started it with different settings than what Nginx expects. Before assuming there are permission issues, as the original query suggested, verify that the service is actually running and listening on the correct interface.

Check if the PHP-FPM master process is active:

ps aux | grep php-fpm
# or
systemctl status php-fpm

If the process isn't running, the connection will always be refused. However, even if the process exists, it might not be listening on 127.0.0.1. By default, many Linux distributions configure PHP-FPM to listen on a Unix socket (e.g., /run/php/php-fpm.sock) rather than a TCP port. If your Nginx configuration is configured for fastcgi://127.0.0.1:9000 but the daemon is waiting on a socket, the connection will fail immediately.

To resolve this, you must ensure the upstream address in your Nginx configuration matches the socket path or TCP port defined by PHP-FPM. If you choose to use a Unix socket, update the fastcgi_pass directive in your Nginx server block:

location / {
    try_files $uri $uri/ =404;
    
    fastcgi_pass unix:/run/php/php-fpm.sock;
    
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Conversely, if you prefer the TCP approach and want to stick with 127.0.0.1:9000, ensure your PHP-FPM configuration explicitly binds to that port. In the php-fpm.conf or a specific pool definition (like www.conf), look for the listen directive:

listen = 127.0.0.1:9000

If you find listen = unix:/run/php/php-fpm.sock there, change it to the TCP address or create a new pool with the TCP setting. Note that switching from socket to TCP can introduce performance considerations; sockets are generally faster for local inter-process communication because they avoid the overhead of the TCP stack and potential port contention issues.

Permissions are indeed a factor, but usually secondary to the listening address mismatch. If PHP-FPM is running as a specific user (e.g., www-data or nobody) and Nginx is trying to connect via a socket, ensure that file permissions on the socket allow the Nginx worker process to access it. Typically, sockets are created with mode 0660 owned by root:www-data. If your PHP-FPM runs as a different user than expected, or if the directory permissions are too restrictive, the connection attempt might fail silently or be refused depending on the kernel's security context.

Another frequent pitfall occurs during deployment scripts. It is easy to configure Nginx to forward traffic to port 9000 while forgetting to execute service php-fpm start or encountering a syntax error in the PHP configuration that prevents the daemon from initializing. Always validate your application stack components before assuming the network configuration is at fault.

If you are building a modern Laravel deployment, ensuring these layers communicate correctly is foundational. While this specific error relates to raw FastCGI, similar upstream failures happen frequently when configuring reverse proxies for various frameworks. For more comprehensive guides on setting up production-ready environments with Nginx and PHP, resources like https://laravelcompany.com offer detailed breakdowns of deployment strategies that can help avoid these configuration mismatches in the first place.

Ultimately, the fix lies in aligning the fastcgi_pass directive with the actual listen directive of your PHP-FPM pool. Once both are pointing to the same logical endpoint—whether a socket or a TCP port—and the process is actively listening, the 502 error will vanish.

Our network