I'm trying to use .htpasswd to protect an /admin/ section on my domain.
I'm able to successfully protect the root index.php file using this config:
location /admin/ {
try_files $uri $uri/ =404;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
But the associated files within that folder are still viewable. For instance, on http://domain.com/admin/whatever.php -- the page loads, then the Nginx password auth comes up, but you can simply cancel out of it and still view the page.
After doing some research, I've tried to use regex wildcards unsuccessfully.
Doesn't work:
location "~^/admin/.$" {
try_files $uri $uri/ =404;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Doesn't work:
location "~^/admin/*\$" {
try_files $uri $uri/ =404;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
How can I password protect both the root index, and any sub folders and files as well? Everything past /admin/ should be inaccessible.