首页 文章

Nginx总是将Laravel路由重定向到默认的根index.php页面

提问于
浏览
1

我是一个新的Laravel学习者 . 在我的Mac(macOS 10.13)上,我配置了Nginx,PHP和MySQL环境 . 首先,Nginx localhost:8080 / laravel / public显示laravel欢迎页面,没有任何问题 . 但是当我尝试添加自定义路线时,例如:

Route::get('test', function () {
    return 'Hello World!';
});

我在localhost上获得了404页面:8080 / laravel / public / test .

在我用Google搜索路径404问题的解决方案之后,我通过添加修改了我的nginx conf文件

try_files $uri $uri/ /index.php?$query_string

如下:

server {
    ...

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

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

现在是故事,404问题已得到修复 . 但我仍然无法访问正确的路由页面 .

当我再次打开localhost:8080 / laravel / public / test时, the browser takes me to the nginx docroot page (that is, localhost:8080/index.php).

我在laravel主文件夹中尝试了 php artisan serve ,使用该命令localhost:8000 / test可以使用"Hello World!"文本正确访问 .

更新:

我刚刚在localhost:8080 /之后尝试了一些其他字符串,比如localhost:8080 / abc之类的东西,似乎任何子路径都会把我带到localhost:8080 / index.php页面!在 try_files $uri $uri/ /index.php?$query_string 之后添加 . 如果我删除这行代码,localhost:8080 / abc将显示404页面(这应该是正确的,因为我在根文件夹中没有abc.php文件) .

似乎在添加之后

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

Nginx将所有未识别的网址重定向到默认主页?

有人能告诉我为什么在添加 try_files $uri $uri/ /index.php?$query_string 之后我的Nginx无法使用Laravel路线吗?

如果您需要更多细节,请告诉我 . 非常感谢!

1 回答

  • 1

    嗯..我通过修改我的Nginx站点配置文件来解决这个问题,改变了

    server {
    
        ...
    
        root         /var/www;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    server {
    
        ...
    
        root         /var/www/laravel/public;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    是的,只需 change the root path to laravel's public folder 然后重启nginx服务器 .

    现在 Hello World! Hello World!

相关问题