2 min read

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

While setting up a new VPS for a client I came across an error - "connect() failed (111: Connection refused) while connecting to upstream". I couldn't quite pinpoint the exact cause and fix on the spot. Thus, I had to go back to my notes and dig it up. Thankfully, it was an easy solution.

Just recently while setting up a new Virtual Private Server (VPS) with Ubuntu 20.04 for a client I came across an error that while may seem common for some, was actually something that I hadn't come across in a few months. The moment I saw the error "connect() failed (111: Connection refused) while connecting to upstream" I got this feeling that I knew exactly what this error was, but I just couldn't quite pinpoint what the solution was. Thus, I had to go back to my notes and dig it up.

The Error

The error in the log displayed like below, but may vary slightly for you.

[error] 20816#20816: *1672 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.xxx, server: example.com, request: "GET / HTTP/1.1", upstream: "", host: "example.com"

If it is the same error that I had experienced, you will also see a 502 Bad Gateway error in your browser when trying to access your site.

What does this error mean?

This error (in my case) was caused by the listen variable in the PHP configuration. This variable was defaulted to listening through a socket and the listen with the 127.0.01 IP was commented out (i.e. not listening on localhost).

How to fix this error?

This error, thankfully, is an incredibly easy fix. The first step you will need to do is access a PHP configuration file with the below command.

Edit (fix) the PHP configuration file

Note: The VPS I was working on had php7.4 installed. If you are using a different PHP version, make sure to adjust the command to account for the version of PHP you are working with.

sudo nano /etc/php/php7.4/fpm/pool.d/www.conf

Once you are in the file, look for the below line in the file and uncomment it.

listen =

You will then want to edit it so it looks like this (with whatever port you need).

listen =

Note: You should also ensure the below line is also commented out if you are connecting through localhost ( and a port.

;listen = /run/php/php7.4-fpm.sock

Next, save and close the file.

Restart Services

You will need to restart the PHP and NGINX services on your server for the changes to take effect properly. First, start with PHP.

sudo service php7.4-fpm restart


sudo service php7.4-fpm reload

Then restart NGINX.

sudo service nginx restart


sudo service nginx reload

You should now be able to access your site if all of the changes have taken effect.

Alternative Issues

The Reverse (need sockets, not IP and Port)

Users may also see a similar error if they are using sockets (i.e. the line we had commented out earlier in the tutorial).

If you are, use the same steps as above, but instead uncomment out the listen with the sockets and comment out the listen with the IP and port like below.

listen = /run/php/php7.4-fpm.sock

;listen =

PHP does not allow to connect

Another issue I have had in the past is that the listen.allowed_clients variable in the PHP configuration was not set. Thus, PHP was preventing FastCGI clients from connecting over (localhost).

This was solved by uncommenting and ensuring the below line specified (localhost) as an allowable FastCGI client IP address.

listen.allowed_clients =

Should you have any trouble, send me a message and I'll do my best to help out.