To ensure that the testdir match is chosen instead of the jpg/txt match, use the following locations:
location ^~ /testdir {
deny all;
return 404;
}
location ~* ^.+\.(jpg|txt)$ {
root /var/www/site;
}
In your example, you have two types of locations. location /testdir
is a prefix location, as it has no tilde (~
) between location
and /testdir
.
location ~* ^.+\.(jpg|txt)$
is a regex location (a case-insensitive one, due to the *
directly after the tilde). From the nginx documentation:
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
The problem here, is that your testdir location is being remembered, but then the jpg/txt location is selected during the regex stage, as it matches. The following note from the documentation is what I based my solution (given above) upon:
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.