Laravel 在 Nginx 404 上

Laravel on Nginx 404

我的 centos 7 中有两个站点 VPS。一个是 laravel 5.6(比方说,def.com),另一个是静态 html 站点(比方说,abc.com)。

html 站点使用此配置 运行 顺利: 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;
        }
    }

但是当我调整 laravel 项目的配置时,如下所示,它带有 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;
        }
    }

我正在使用这个指南https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-on-centos-7

尝试将 root 和 index 指令放在 server 块中(在 location 块之外),并将 try 文件修改为:try_files $uri $uri/ /index.php?$query_string;

另外你在第 3 行有两次 server_name。

您可以在此处找到综合指南:https://www.digitalocean.com/community/tutorials/how-to-deploy-a-laravel-application-with-nginx-on-ubuntu-16-04

替换此

try_files $uri $uri/ =404;

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

之后运行

sudo service nginx restart