I need to restrict access to any files or subdirs in direstory "testdir". My conf:
...
location ~* ^.+\.(jpg|txt)$ {
root /var/www/site;
}
location /testdir {
deny all;
return 404;
}
...
In my configuration I have no restrictions on /testdir/jpg_or_txt-files. How to do it?
to restrict access to multiple directories in nginx in one location entry do
It's because the "root" directive matches before the "deny" directive can be matched. Reverse the order of your directives and it should work:
To ensure that the testdir match is chosen instead of the jpg/txt match, use the following locations:
In your example, you have two types of locations.
location /testdir
is a prefix location, as it has no tilde (~
) betweenlocation
and/testdir
.location ~* ^.+\.(jpg|txt)$
is a regex location (a case-insensitive one, due to the*
directly after the tilde). From the nginx documentation: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:
This will give you a 403 all the time because of the deny all... When you want the server to give you a 404 just only return a 404... like so: