Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

502 Bad Gateway for Laravel 5.4 with nginx and php7.0-fpm in Ubuntu

I have my Laravel 5.4 app setup in Ubuntu 16.04 server with nginx and php7.0-fpm, it gives

502 Bad Gateway

Nginx virtualhost config,

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /var/www/html/laravel/public;
    index index.php index.html;

    # Make site accessible from http://localhost/
    server_name localhost;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        include                  fastcgi_params;
        fastcgi_keep_conn on;
        fastcgi_index            index.php;
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php7.0-fpm.sock;
    }
}

Tried the following but still not working,

Changed fastcgi_pass unix:/var/run/php7.0-fpm.sock; to fastcgi_pass 127.0.0.1:9000;

Changed try_files $uri $uri/ /index.php?$query_string; to try_files $uri $uri/ /index.php$is_args$args;

Restarted service after each change,

service nginx restart
service php7.0-fpm restart

I can access only the main route with this config,

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/html/laravel/public;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}
like image 917
Anshad Vattapoyil Avatar asked Jan 28 '17 08:01

Anshad Vattapoyil


People also ask

How do I fix 502 Bad Gateway nginx ubuntu?

Disable all installed plugins and extensions and check the connection again. Change your DNS server. Changing the DNS server may resolve the 502 error. You can choose Open DNS or Google DNS servers.

What causes 502 Bad Gateway nginx?

In more technical words, A 502 Bad Gateway means that the proxy (gateway) server wasn't able to get a valid or any response from the upstream server. If you are seeing a 502 bad gateway error on a website, it means that the origin server sent out an invalid response to another server that acted as a gateway or proxy.


2 Answers

Updating one line with default php based config worked,

server {
        listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www/html/laravel/public;
        index index.html index.htm index.php;

        # Make site accessible from http://localhost/
        server_name localhost;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

Here changed try_files $uri $uri/ =404; to try_files $uri $uri/ /index.php?$query_string;

like image 162
Anshad Vattapoyil Avatar answered Oct 12 '22 13:10

Anshad Vattapoyil


First install: sudo apt install php-fpm

Then check /etc/php/7.4/fpm , Make sure your version of php7.0-fpm.sock, php7.4-fpm.sock or 7.x

fastcgi_pass unix:/run/php/php7.4-fpm.sock;
like image 44
Infomaster Avatar answered Oct 12 '22 12:10

Infomaster