Report this

What is the reason for this report?

Connection refused Nginx

Posted on August 1, 2021

As I am trying to install a Droplet with LEMP and running my Laravel Project on it. It gives me a 502 Bad Gateway then I went to the log file and saw this error:

2021/08/01 11:30:17 [error] 29534#29534: *13 connect() failed (111: Connection refused) while connecting to upstream, client: MY_IP_ADDRESS, server: DOMAIN_NAME, r>

My config file looks like these:

    server_name DOMAIN_NAME;
    root /var/www/DOMAIN_NAME/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ .php$ {
       root /var/www/DOMAIN_NAME/public;
       fastcgi_split_path_info ^(.+\.php)(.*)$;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /var/www/DOMAIN_NAME$fastcgi_script_name;
       include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Please can someone explain what I am doing wrong here?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hi there,

The 502 error indicates that the backend service that you are trying to connect to is either not running or the Nginx configuration is not correctly set to connect to that backend service.

In your case you are using PHP-FPM as your backend service, so you would need to make sure that the PHP-FPM service si actually running:

sudo systemctl status php7.4-fpm.service

Note: change 7.4 with the PHP version that you are running.

If the service is actually running, than I could suggest following the steps here on how to setup Nginx to connect to PHP-FPM:

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

The 502 Bad Gateway error typically means that Nginx, acting as a reverse proxy, is unable to connect to the upstream server (in this case, PHP-FPM). The specific error you’re seeing (connect() failed (111: Connection refused) while connecting to upstream) suggests that Nginx is unable to connect to PHP-FPM.

More often than not, the error is related to a problem with the Application rather than anything else. In such cases checking the logs of the application is better suited.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.