I'd like to match question mark "?" as regexp on nginx.conf location.
For example, a URL pattern which I'd like to match is /something?foo=5 or /something?bar=8 (parameter only changeable).
Because nginx adopts RCPE, I can write the location on nginx.conf as follows:
location ~ ^/something\?.* {
}
The above doesn't match the URL pattern. 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.
NGINX location directive syntax The NGINX location block can be placed inside a server block or inside another location block with some restrictions. The syntax for constructing a location block is: location [modifier] [URI] { ... ... } The modifier in the location block is optional.
The location directive within NGINX server block allows to route request to correct location within the file system. The directive is used to tell NGINX where to look for a resource by including files and folders while matching a location block against an URL.
NGINX allows regexes in multiple parts of a configuration, for example locations, maps, rewrites, and server names. The tester described here is for regexes in locations and maps.
nginx location block doesn't match query string at all. So it's impossible. This directive allows different configurations depending on the URI.
nginx location block doesn't match query string at all. So it's impossible.
Location
This directive allows different configurations depending on the URI.
In nginx, there is a built-in variable $uri, which the location block is matched against. For example, give a request
http://www.example.com/app/login.php?username=xyz&password=secret
the $uri value is this string:
/app/login.php
and the query_string is stored in nginx variable $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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With