Laravel在Nginx 404上

时间:2018-07-19 05:03:28

标签: php laravel nginx lemp

我的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

2 个答案:

答案 0 :(得分:1)

替换此

try_files $uri $uri/ =404;

使用

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

运行之后

sudo service nginx restart

答案 1 :(得分:0)

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

在第三行,您也有两次server_name。

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