nginx位置正则表达式规则不起作用

时间:2018-02-13 19:15:19

标签: regex nginx

我们正在尝试在nginx端创建一个位置规则,但它没有按预期工作。以下是位置规则

 location ~* ".*\.legalcontent\.html\?path=\/legal\/.*" {
     return 200 'regex rule'; //Using this temporarily to confirm if rule is triggering
 }

传入网址在下方,想要捕获( .legalcontent.html?path = / legal /

https://www.mycompany.com/myapp/myproduct/mysubpage.legalcontent.html?path=/legal/somepage

regex tool上单独测试正则表达式,似乎很好

enter image description here

但它不起作用,尝试了简单的上下文根来检查其他一切是否正常。

# Following simple rules works fine, but we want location to kick-in 
# only for specific condition
location /myapp {
    return 200 'myapp rule';
}

location /myapp/myproduct {
    return 200 'myapp myproduct rule';
}

是否有办法处理此类输入网址的位置规则

1 个答案:

答案 0 :(得分:1)

location只会检查请求的路径。要检查参数,您必须使用$args变量创建条件。

location ~* ".*\.legalcontent\.html$" {
    if ($args ~* "^path=\/legal\/.*") {
        return 200 'regex rule';
    }
}
相关问题