简体   繁体   English

Nginx正则表达式位置

[英]Nginx regular expression location

This regular expression doesn't work. 此正则表达式无效。

Below is the one. 下面是一个。

    location ~* /a2aEx\?.*$ {

    }

I requested to /a2aEx?AID=ASA_1&BID=1 but NGINX respond with 404 status 我请求/a2aEx?AID=ASA_1&BID=1但NGINX回复404 status

However if I fix it to below, it works fine, returning 200 status 但是,如果我将其修复到下面,则可以正常工作,返回200 status

    location ~* /a2aEx.*$ {   # remove `\?`

    }

I verify my regular expression on Python with below code. 我使用以下代码在Python上验证我的正则表达式。

 text = "/a2aEx?AID=ASA_1&BID=1"

 reObj = re.match("/a2aEx\?.*$", text)
 print reObj.string  # it returns `/a2aEx?AID=ASA_1&BID=1`

Seeing the symptom, the escape char( \\ ) doesn't work as I expect. 看到症状,转义char( \\ )不能正常工作。

Your regular expression is fine. 您的正则表达式很好。 The issues lies with how Nginx is examining the incoming request. 问题在于Nginx如何检查传入的请求。

Nginx treats anything after ? Nginx之后会?对待? as a query string in the request. 作为请求中的查询字符串。 location blocks match against the $uri variable ( /a2aEx ) in your example. 在您的示例中, location块与$uri变量( /a2aEx )相匹配。 Location blocks do not examine the $args variable, which is what you are attempting to match against. 位置块不会检查$args变量,而这正是您要匹配的变量。

Your second example matches the $uri variable correctly. 第二个示例正确匹配$uri变量。

To operate on the arguments, you can use Nginx's if operator, but it is generally discouraged. 要对参数进行运算,可以使用Nginx的if运算符,但通常不建议这样做。

An escape problem? 逃生问题?

Try one of the following: 请尝试以下方法之一:

  • double the backslash 反斜杠加倍

     location ~* /a2aEx\\\\?.*$ { 
  • put the regex in quotes 将正则表达式放在引号中

     location ~* "/a2aEx\\?.*$" { 
  • replace by a character class 用角色类代替

     location ~* "/a2aEx[?].*$" { 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM