Home  >  Article  >  Backend Development  >  nginx rewrite规则的一些问题,关于last和break的差异

nginx rewrite规则的一些问题,关于last和break的差异

WBOY
WBOYOriginal
2016-06-06 20:24:361256browse

是这样的,我想实现一个url:example.com/test.php/a/b/c ,web目录下有test.php文件,想接收/a/b/c作为路由参数实现路由功能。
如果不设置rewite规则的话,打开example.com/test.php是可以的,但是打开example.com/test.php/a/b/c会报404。然后我就想自己rewrite规则。
尝试了很多次之后,发现

<code>location /test.php {
    rewrite . /test.php last;                                                                                                                                                                                 
}</code>

这样可以使页面正常访问,而且php的$_SERVER['REQUEST_URI']是test.php/a/b/c,能够达到我的需求。但是把last改到break就不对了。

我想知道为什么last会可以成功rewrite,已经把url rewrite到/test.php了,为什么后面的/a/b/c/还是能够保留?而且为什么用了break就不行。
而且last这个flag,不会停止location的匹配啊,test.php被rewrite到test.php应该还是会进入这个location并且无限循环返回500才是。为什么这样反而能够正常运行了。
虽然已经实现了我想要的功能,但是实在想不通这是什么原因。

回复内容:

是这样的,我想实现一个url:example.com/test.php/a/b/c ,web目录下有test.php文件,想接收/a/b/c作为路由参数实现路由功能。
如果不设置rewite规则的话,打开example.com/test.php是可以的,但是打开example.com/test.php/a/b/c会报404。然后我就想自己rewrite规则。
尝试了很多次之后,发现

<code>location /test.php {
    rewrite . /test.php last;                                                                                                                                                                                 
}</code>

这样可以使页面正常访问,而且php的$_SERVER['REQUEST_URI']是test.php/a/b/c,能够达到我的需求。但是把last改到break就不对了。

我想知道为什么last会可以成功rewrite,已经把url rewrite到/test.php了,为什么后面的/a/b/c/还是能够保留?而且为什么用了break就不行。
而且last这个flag,不会停止location的匹配啊,test.php被rewrite到test.php应该还是会进入这个location并且无限循环返回500才是。为什么这样反而能够正常运行了。
虽然已经实现了我想要的功能,但是实在想不通这是什么原因。

last在匹配到规则后,会跳转到这条规则里面进去。
break在匹配到规则后则不会。
这就是两者的区别

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn