一个很简单的nginx重写规则, 完全看不明白为什么重写到正确的路径的

www.domain.com/test.php/a/b

location / {
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=$1  last;   break;
    }
}

就这个重写规则, 不知道为什么会重写到 test.php
实际效果与 www.domain.com/test.php?s=/a/b 效果一致
我尝试多个版本nginx, 无法重现.

nginx -T

未看到有什么特殊设置.

我的开发环境, 为了能够打开这个url http://www.domain.com/test.php/a/b, 做了如下设置

location /test.php/ {
    rewrite  ^/test.php(.*)$  /test.php?s=$1  last;
}

其他同事并无任何特殊设置

这是 test.php 代码

<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// [ 后台入口文件 ]// 使用此文件可以达到隐藏admin模块的效果
// 建议将admin.php改成其它任意的文件名,同时修改config.php中的'deny_module_list',把admin模块也添加进去
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');

// 判断是否安装FastAdmin
if (!is_file(APP_PATH . 'admin/command/Install/install.lock'))
{
 header("location:./install.php");
 exit;}

// 加载框架引导文件
require __DIR__ . '/../thinkphp/base.php';

// 绑定到admin模块
\think\Route::bind('admin');

// 关闭路由
\think\App::route(false);

// 设置根url
\think\Url::root('');

// 执行应用
\think\App::run()->send();
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 7

你的项目根目录下有test.php吗?有的话就不会走这里的重写规则,会直接进入test.php中继续执行。没有的话,会将请求转发到index.php文件,然后在index.php中执行。所以,你说的「重写到 test.php」是不存在的。。

3年前 评论
liux156

test.php这个文件, 但是我无法重现, 只是在别人服务器中有效, 本地搭建的环境, 只会重写到index.php

所以, 我无法理解, 为什么会重写到test.php

按照我的理解
http://www.domain.com/test.php/a/b
应该是检测到是否存在 /test.php/a/b 这个文件是否存在, 如果不存在, 则进入到重写规则.
不应该会检测 /test.php 这个文件.

3年前 评论
LiamHao 3年前
liux156 (作者) (楼主) 3年前
LiamHao 3年前
liux156 (作者) (楼主) 3年前
LiamHao 3年前

这是先判断文件是否存在,文件不存在再重写到index.php。是判断文件,不会包含后面的/a/b,就检测test.php是否存在

3年前 评论

.php转义一下看看

location /test.php/ {
    rewrite  ^/test\.php(.*)$  /test.php?s=$1  last;
}
3年前 评论
陈先生
 if (!-e $request_filename) {  //  如果文件不存在 注意是不存在  
        rewrite  ^(.*)$  /index.php?s=$1  last;   break; //转发到index.php?s=路由参数 结束本次请求的转发行为 
    }
//如果你的test.php存在,则会继续执行 不存在才会转发给index.php 
2年前 评论
liux156 (楼主) 2年前
liux156

不是应该判断test.php/a/b 是否存在吗?这里不回拆分判断吧? 不会判断 test.php,也不会判断 test.php/a,只会判断test.php/a/b是否存在

2年前 评论

这个,是不是配置 什么模式了,比如调试模式, 然后由index.php 跳转到test.php, 这些不可以打点试一下吗?

2年前 评论
liux156 (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!