Question

*137 connect() failed (111: Connection refused) while connecting to upstream, client:

Posted June 9, 2021 611 views
NginxPHPNode.js

I know this question has been asked a lot but I’ve spent the last 3 hours on it and I don’t want to keep all the fun to my self.

Long story short I need node js and php on the same droplet, (I need to serve the php as the web client “front end”) and node for the api.
I wrote my raw php as a MVC and have the index.php serving from root/public and serving the public ip of the droplet

  1. php7.4-fpm
  2. nginx/1.14.2
  3. node v12.22.1

This is my current server block in nginx:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html/public;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name http://xxx.xxx.xxx.xxx;

        location / {
                try_files $uri $uri/ =404;
        }
    location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
        }

        location ~ /\.ht {
                deny all;
        }

And the netstat -plant:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      28551/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      617/sshd
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      7762/postgres
tcp        0    340 xxx.xxx.xxx.xxx:22       xxx.xxx.xxx:46241       ESTABLISHED 851/sshd: root@pts/
tcp        0      0 xxx.xxx.xxx.xxx:22       xxx.xxx.xxx:18432       ESTABLISHED 717/sshd: root@nott
tcp6       0      0 :::80                   :::*                    LISTEN      28551/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      617/sshd

When I try and connect my nginx error log shoots me: *137 connect() failed (111: Connection refused) while connecting to upstream, client:

Do I have to run php -s? Shouldn’t fpm handle spinning the service up?

Any suggestions?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there @keelingwesley,

It looks like that you’ve set the fastcgi_pass to 127.0.0.1:9000, however from the netstat output, it looks like that FPM is not listening on that port.

It is possible that your FPM service is not listening on a TCP socket but on a UNIX socket. You can check that in your config file here:

/etc/php/7.4/fpm/pool.d/www.conf

Note: change the 7.4 with the PHP version that you are using

There look for the following line:

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

The listen value needs to match the fastcgi_pass that you’ve specified in your Nginx configuration. This is how Nginx is going to communicate with PHP-FPM.

If you have a UNIX socket specified as the listen value, you need to update your Nginx configuration as follows:

                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

For more information on how to setup PHP-FPM with Nginx, you could take a look at this tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04

Let me know how it goes.
Regards,
Bobby

by Erika Heidi
The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications written in PHP. This is an acronym that describes a Linux operating system, with an Nginx (pronounced like “Engine-X”) web server. The backend data is stored in a MySQL database, and dynamic processing is handled by PHP. In this guide, we'll set up a LEMP stack on an Ubuntu 20.04 server.