nginx 重写后,laravel 路由显示404
1. 运行环境
laravel/framework: “^8.75”
PHP 版本:8.0
nginx 版本:1.15.11
2. 问题描述
nginx 配置如下
server {
listen 93;
server_name localhost;
root "F:/project/work/gameWeb/public/";
location /m/ {
rewrite ^/m/(.*)$ /index.php$1 break;
}
location / {
index index.php index.html error/index.html;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
laravel 路由配置文件如下
Route::get('/register', 'Auth\RegisterController@showRegistrationForm');
我访问路径:localhost:93/register 正常
访问路径:localhost:93/m/register 会出现路由匹配不上,404的问题
3. 个人理解
我的个人理解是,nginx的rewrite重写,并不会改变$_SERVER[‘REQUEST_URI’] 的值,也就是说,rewrite虽然将 /m/register 重写成了 /register,但是程序是拿 $_SERVER[‘REQUEST_URI’] 的值进行路由对比,但是重写并未改变 $_SERVER[‘REQUEST_URI’] ,所以拿 /m/register 对比路由会匹配失败,从而导致404的产生
我没去阅读源码,不知道我的理解对不对?
推荐文章: