Question

Connection refused Nginx

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?


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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