Url rewriting 使用Nginx URL重写系统。。简单,最后还是休息?

Url rewriting 使用Nginx URL重写系统。。简单,最后还是休息?,url-rewriting,nginx,Url Rewriting,Nginx,我正在努力改变 http://site.com/show_page.php?name=terms 到 我通常对Nginx非常好,在过去,我已经设置了几个站点并做了一些工作。这是我的URL重写,在conf vhost中-我尝试用last替换break,但没有成功 location /pages { rewrite ^/pages/(.*)$.html /show_page.php?name=$1? break; } 它不起作用有两个原因。首先,您的重写规则不正确,请将其更改为: re

我正在努力改变

http://site.com/show_page.php?name=terms

我通常对Nginx非常好,在过去,我已经设置了几个站点并做了一些工作。这是我的URL重写,在conf vhost中-我尝试用
last
替换
break
,但没有成功

location /pages {
    rewrite ^/pages/(.*)$.html /show_page.php?name=$1? break;
} 

它不起作用有两个原因。首先,您的重写规则不正确,请将其更改为:

rewrite ^/pages/(.*).html$ /show_page.php?name=$1? last;
第二个是,当你像这样重写时,nginx不知道如何处理php文件,因为它永远不会到达
location~\.php
块。您可以通过将完整的
位置~\.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;
}

它不起作用有两个原因。首先,您的重写规则不正确,请将其更改为:

rewrite ^/pages/(.*).html$ /show_page.php?name=$1? last;
第二个是,当你像这样重写时,nginx不知道如何处理php文件,因为它永远不会到达
location~\.php
块。您可以通过将完整的
位置~\.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;
}

你对账单上的美元符号不属于那里。美元符号表示字符串的结尾。因此,没有什么能成为一场成功的比赛。对于其余部分,您的重写是正确的,您可以忽略jagsler关于无法找到php语句的评论。这是不正确的,因为文档中已明确说明,最后一条指令将指示nginx搜索要匹配的新位置。由于语句将URL重写到与所在位置块不匹配的其他位置,因此也没有循环的机会。

语句中的美元符号不属于该位置。美元符号表示字符串的结尾。因此,没有什么能成为一场成功的比赛。对于其余部分,您的重写是正确的,您可以忽略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,但不要跳转,而是停留在同一位置块中,继续到块内的下一行 但我们必须记住这一点:

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,但不要跳转,而是停留在同一位置块中,继续到块内的下一行

这就是我的想法。最初我对关闭和移动PHP有点迟疑。。。也许他不明白。非常感谢您的解释。
location/pages{rewrite^/pages/(.*).html/show_page.php?name=$1?break;}
这就是您的意思,请发布示例这就是我的想法。最初我对关闭和移动PHP有点迟疑。。。也许他不明白。非常感谢您的解释。
location/pages{rewrite^/pages/(.*).html/show_page.php?name=$1?break;}
这就是您的意思,请发布示例