This is the URL I need to match.
https://example.com/wp-login.php
https://example.com/wp-login.php?action=lostpassword
https://example.com/?s=
https://example.com/?s=xxxxxxx
This is the location matching rule I am using, it does not work, what is wrong please?
location ~* ^/(?:wp-login.php|?s=) {
deny all;
}
2
Answers
With your shown samples and attempts please try following regex.
Here is the Online demo for regex.
Explanation: Adding detailed explanation for above.
This is the common nginx novices error. Neither
location
norrewrite
directives works with the whole request URI. Both of them works with the normalized request URI which does not include the query string part at all (description of what URI normalization is can be found in thelocation
directive documentation). That means you can’t check the query string usinglocation
directive at all.Usually what you are asking for can be achieved checking the
$arg_<name>
nginx variable value, something likeUnfortunately there is no way to distinct an empty query argument value from the absence of such an argument (well, at least without using third party modules like lua-nginx-module, where that difference can be actually checked against an empty string or a
nil
value). Fortunately an original request URI is available to you via the$request_uri
internal variable, so you can do something likeor even use a single
$request_uri
variable matching against a regex pattern like:(this should be placed at the
server
configuration level)