[英]Using Nginx URL Rewrite System .. simple, last or break?
我想改变
http://site.com/show_page.php?name=terms
至
http://site.com/pages/terms.html
我通常很擅长Nginx设置了几个网站,并在过去完成了一些工作。 这是我的URL重写,在conf vhost中 - 我尝试用last
替换break
但没有运气。
location /pages {
rewrite ^/pages/(.*)$.html /show_page.php?name=$1? break;
}
您的对帐单中的美元符号不属于那里。 美元符号表示字符串的结尾。 因此,在成功比赛之后没有任何事情可以发生。 对于其余的你的重写是正确的,你可以忽略jagsler关于无法找到php语句的评论。 文档清楚地说明这是不正确的,最后一条指令将指示nginx搜索匹配的新位置。 由于语句将URL重写到与其所在的位置块不匹配的不同位置,因此也没有循环的机会。
jagsler的答案还可以,但必须牢记这一点:
server {
# you config here (servername, port, etc.)
location /pages {
#***modified . = any character, so escape literal dots***
rewrite ^/pages/(.*)\.html$ /show_page.php?name=$1? last;
#***the line bellow will only be executed if the rewrite condition***
#***equals false, this is due to the "last" modifier in the rewrite rule***
include php.conf;
}
# instead of the php location block also just add the include
include php.conf;
}
因此,请注意重写规则中“修饰符”的行为“last”表示如果重写条件等于true,则重写请求的uri并跳转到适合新重写的uri的位置块。
另一个修饰符是“break”,这意味着如果重写条件等于true则重写请求的uri但不要JUMP,而是保持在相同的位置块并继续到块内的下一行
有两个原因它不起作用。 首先,您的重写规则不正确,将其更改为:
rewrite ^/pages/(.*).html$ /show_page.php?name=$1? last;
第二个是当你像这样重写时,nginx不知道如何处理php文件,因为它永远不会到达location ~ \\.php
块的location ~ \\.php
。 您可以通过将完整location ~ \\.php
放在名为php.conf(或您喜欢的任何名称)的不同文件中来解决此问题,并将其包含在您需要的服务器块中。
这可能看起来像:
server {
# you config here (servername, port, etc.)
location /pages {
rewrite ^/pages/(.*).html$ /show_page.php?name=$1? last;
include php.conf;
}
# instead of the php location block also just add the include
include php.conf;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.