简体   繁体   English

如何在nginx.conf位置匹配问号“?”作为regexp

[英]How to match question mark “?” as regexp on nginx.conf location

I'd like to match question mark "?" 我想匹配问号“?” as regexp on nginx.conf location. 作为nginx.conf位置的正则表达式。

For example, a URL pattern which I'd like to match is /something?foo=5 or /something?bar=8 (parameter only changeable). 例如,我想匹配的URL模式是/某些?foo = 5或/ something?bar = 8(参数只能更改)。

Because nginx adopts RCPE , I can write the location on nginx.conf as follows: 因为nginx采用RCPE ,我可以在nginx.conf上写如下位置:

location ~ ^/something\?.* {
}

The above doesn't match the URL pattern. 以上与URL模式不匹配。 How can I do that? 我怎样才能做到这一点?

Also, the following is not my expectation. 此外,以下不是我的期望。

location ~ ^/something?.* {
}

It'll match /something_foo_bar_buzz that I don't expect. 它会匹配/ something_foo_bar_buzz我不指望。

nginx location block doesn't match query string at all. nginx位置块根本不匹配查询字符串。 So it's impossible. 所以这是不可能的。

Location 地点

This directive allows different configurations depending on the URI. 该指令允许根据URI进行不同的配置。

In nginx, there is a built-in variable $uri, which the location block is matched against. 在nginx中,有一个内置变量$ uri,匹配位置块。 For example, give a request 例如,提出请求

http://www.example.com/app/login.php?username=xyz&password=secret

the $uri value is this string: $ uri值是这个字符串:

 /app/login.php

and the query_string is stored in nginx variable $args: query_string存储在nginx变量$ args中:

username=xyz&password=secret

To do something wrt. 做某事。 query string, you can do something like 查询字符串,你可以做类似的事情

if ($args ~ username=xyz) {
   # do something for requests with this query string
}

But be careful, IF is Evil 但要小心, IF是邪恶的

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

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