5

I have two sites in my centos 7 VPS. One is laravel 5.6 (let say, def.com) and the other is static html site (let say, abc.com).

The html site is running smoothly with this config: sites-available/abc.com.conf

    server {
        listen   80;
        server_name  server_name abc.com www.abc.com;

        # note that these lines are originally from the "location /" block
        #root   /var/www/abc.com/html;
        #index index.php index.html index.htm;

        location / {
            root   /var/www/abc.com/html;
            index index.php index.html index.htm;
            try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

But when I am adjust the config for laravel project, like below, it come with 404

sites-available/def.com.conf

    server {
        listen   80;
        server_name  server_name def.com www.def.com;

        # note that these lines are originally from the "location /" block
        #root   /var/www/def.com/public;
        #index index.php index.html index.htm;

        location / {
            root   /var/www/def.com/public;
            index index.php index.html index.htm;
            try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

I am using this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-on-centos-7

Nyicip
  • 170
  • 1
  • 2
  • 11

3 Answers3

18

replace this

try_files $uri $uri/ =404;

with

try_files $uri $uri/ /index.php$is_args$args;

after that run

sudo service nginx restart
iHabboush
  • 537
  • 4
  • 8
0

Try to put the root and index directives in the server block (outside of the location block), and modify the try files to: try_files $uri $uri/ /index.php?$query_string;

Also you have the server_name two times on the 3rd line.

You can find a comprehensive guide here: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04

Daniel W
  • 433
  • 3
  • 8
  • I did what you say but it doesn't work. Anyway, I've 2 server_name because of i want to declare with and without www, and it's running smooth with static site. – Nyicip Jul 19 '18 at 09:26
  • I understand that you want it to work with and without www, but I meant you literally wrote twice "server_name server_name" Have you uploaded the laravel app to the server and configured it properly? – Daniel W Jul 19 '18 at 12:06
  • I've tried one server_name and it didn't work. Yes, I've uploaded laravel app to server and configured it properly. – Nyicip Jul 19 '18 at 16:57
0

None of the above worked for my method.

In my case, it was due to insufficient rights. I fixed it as follows.

 chown -R www-data domain_folder.uz
Akbarali
  • 688
  • 7
  • 16