【问题标题】:Laravel on Nginx 404Nginx 404 上的 Laravel
【发布时间】:2019-04-20 01:23:14
【问题描述】:

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

【问题讨论】:

标签: php laravel nginx lemp


【解决方案1】:

替换这个

try_files $uri $uri/ =404;

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

之后运行

sudo service nginx restart

【讨论】:

  • 像魅力一样工作。非常感谢,
【解决方案2】:

尝试将 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

【讨论】:

  • 我按照你说的做了,但是没有用。无论如何,我有 2 个 server_name 因为我想声明有和没有 www,并且它在静态站点上运行顺利。
  • 我知道你希望它在有和没有 www 的情况下都能工作,但我的意思是你字面上写了两次 "server_name server_name" 你有没有将 laravel 应用程序上传到服务器并正确配置它?
  • 我尝试了一个 server_name ,但没​​有成功。是的,我已将 laravel 应用上传到服务器并正确配置。
猜你喜欢
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2015-08-18
  • 2021-06-22
  • 2018-09-26
  • 2018-06-14
相关资源
最近更新 更多