Question

Connection refused Nginx

Posted August 1, 2021 119 views
NginxMySQLPHPLaravelDigitalOcean Droplets

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?

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,

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

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.
  • Hi Bobby,

    First of all thanks for your reply! I have already followed the mentioned link to install my LEMP server.

    After running:

    sudo systemctl status php7.4-fpm.service
    

    It gives me the following output:

    ● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
         Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
         Active: active (running) since Sat 2021-07-31 19:41:38 UTC; 16h ago
           Docs: man:php-fpm7.4(8)
       Main PID: 697 (php-fpm7.4)
         Status: "Processes active: 0, idle: 2, Requests: 25, slow: 0, Traffic: 0req/sec"
          Tasks: 3 (limit: 1136)
         Memory: 10.8M
         CGroup: /system.slice/php7.4-fpm.service
                 ├─697 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
                 ├─760 php-fpm: pool www
                 └─761 php-fpm: pool www
    
    Jul 31 19:41:37 s1-api systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
    Jul 31 19:41:38 s1-api systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
    

    Looks like everything is running?

    • Looks like I have solved the problem above, but it gives me a new error

      2021/08/01 12:27:14 [error] 31089#31089: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: MY_IP_ADDRESS, server: a>
      
      • Hi there,

        I could suggest checking a couple of things:

        • Make sure that your PHP-FPM service is actually listening on port 9000:
        netstat -plant | grep 9000
        

        If this is not the case change the Nginx proxy configuration to match the port

        • Make sure that the PHP-FPM service and the Nginx service are running as the same user.

        Also are you running this on a CentOS Droplet or Ubuntu? If it is on a CentOS server, you might have to run the following to configure SELinux:

        chcon -R -t httpd_sys_content_t /var/www/your_project_name
        

        Alternatively, for Laravel project what I usually do is to use LaraSail which automatically provisions your server configuration:

        https://github.com/thedevdojo/larasail

        Regards,
        Bobby