【问题标题】:Laravel routes are not working only root route is workingLaravel 路由不起作用,只有根路由起作用
【发布时间】:2017-10-31 04:36:38
【问题描述】:

我已经在 EC2 实例上克隆了我的 laravel 项目(类型 ubuntu 16.04) 之后我运行了composer install 命令

我已经根据我的本地机器更改了 /etc/nginx/site-available/default 配置

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/myproject/public;

    charset utf-8;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name myServerIpAddress;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }


    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
}

现在,当我打开我的服务器 IP 地址时,它会正确显示我的 / 路由,但是当我尝试打开 /login/register 时,它显示找不到 404

我的 nginx 版本是 (1.10.0) 我的php版本是(7.0.18)

我也检查了日志文件,但没有显示任何错误

谢谢

【问题讨论】:

  • 您是否按照此处的说明进行操作:laravel.com/docs/5.4/#web-server-configuration?
  • 天哪,谢谢它成功了。只是好奇为什么这个配置在我的本地环境中工作。反正我以后会想办法的。再次感谢

标签: php ubuntu nginx amazon-ec2


【解决方案1】:

使用你当前的配置,当请求/login时,nginx会尝试查看一个名为/var/www/html/myproject/public/login的文件是否存在,然后如果一个名为/var/www/html/myproject/public/login/的文件夹存在,如果它们都不存在则返回错误404 .

这不是你所期望的,因为你没有创建一个名为 login 的文件,你只是在 Laravel 的路由器中创建了一个路由。因此,您必须配置 nginx,使其在每次向该 vhost 请求时调用公共目录的 index.php 文件,然后框架将能够比较目标 URL带有您注册的路线的请求。

As documented on Laravel's website,您应该编辑您的配置以添加以下 location 块,以便将所有请求(静态资源除外)重定向到 index.php,原始查询为参数:

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

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 2015-01-26
    • 2015-03-13
    • 2019-11-03
    • 2018-08-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多