【问题标题】:Nginx configuration for Laravel 5.2 and PHP 7: Routes not workingLaravel 5.2 和 PHP 7 的 Nginx 配置:路由不工作
【发布时间】:2016-04-29 20:21:14
【问题描述】:

我正在尝试设置 AWS Ubuntu 服务器来托管 Laravel 项目。我在让路由正常工作时遇到问题,我怀疑我的 Nginx 虚拟主机配置文件是罪魁祸首。

我认为特别是 try_files 行是问题所在?我试过try_files $uri $uri/ /index.php$is_args$args;try_files $uri $uri/ /index.php?$query_string; 以及其他几个,但有几个错误的结果,包括:

  • 404 错误
  • 500 个错误
  • 正在链接到源代码的下载
  • 这是当前行为,被重定向到 /index.php/(显示 Laravel 欢迎页面)。

这是我的 /etc/nginx/sites-enabled/default:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

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

    # Make site accessible from amazon aws
    server_name xxxx.us-west-2.compute.amazonaws.com;

    location / {
            # Try file, then try folder, then pass to Laravel's /public/index.php
            try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

这是文件的当前状态,就像我说的那样,它当前将任何真实路由重定向到 blahblah.com/index.php/ 并显示 Laravel 欢迎页面。 Laravel 404 页面中未设置的虚假路由。

我很难过,因为有几个指南,而且我已经尝试了所有我能找到的建议,所以我一定错过了一些东西。

看起来 php7 工作正常,不是问题,或者这可能是 php 配置文件中的问题?

发展: 如果我使用它的 IP 访问该站点,它似乎可以正常工作(感谢 Oliver Queen),知道如何在使用域名时让它工作吗?

【问题讨论】:

  • 我的猜测是域名不可能。

标签: php laravel nginx laravel-5.2 php-7


【解决方案1】:

将您当前的location / 替换为以下代码:

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

然后将其下方的位置替换为:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

【讨论】:

  • 就像我说的,已经尝试过这个选项。不过我会幽默你的。更改了文件sudo service nginx restart,重新加载页面,行为没有变化。
  • 对不起@aurath,我提前提交了
  • 请重试上述方法
  • 感谢您的回复。试过了,行为仍然存在。每个有效路由都被重定向到blahblah.com/index.php/ 并显示欢迎页面。无效路由获取 Laravel 的 404 页面。
  • 直接访问IP会怎样?
猜你喜欢
  • 1970-01-01
  • 2017-02-15
  • 2016-07-04
  • 1970-01-01
  • 2016-12-05
  • 1970-01-01
  • 2017-11-15
  • 2015-02-04
  • 1970-01-01
相关资源
最近更新 更多