
So, I have a site, let’s call it example.com. But I have multiple levels of directories under it. I have directories like /example.com/repositories and the actual repositories at the next level like so example.com/repositories/abcd or example.com/repositories/efgh.
Now, I only want access to example.com/repositories restricted. Everyone should be able to access the root level i.e. example.com and the nested subdirectories i.e. example.com/repositories/*.
The way to go about doing this is –
location ~^/repositories/?$ {
deny all;
return 404;
}
location ~^/repositories/.+ {
...
remaining configuration
...
}
The first section restricts access to both example.com/repositories as well as example.com/repositories/ (because ? allows an optional / at the end)
location ~^/repositories/?$ {
deny all;
return 404;
}
The second section ensures example.com/repositories/anything remains accessible.
location ~^/repositories/.+ {
...
remaining configuration
...
}