问题
I am using NGINX to run my website, I wanted to enable authentication to the website, so I did, using .htpasswd file and it worked, here's part of my default.conf file:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
auth_basic "Administrator Login";
auth_basic_user_file /usr/share/nginx/html/.htpasswd;
location / {
root /usr/share/nginx/html;
index index.html index.html;
}
And it works, this way my WHOLE page requires authentication, now I wanted to make 'public' folder available without authentication, so I followed official NGNIX docs and added this directive into my default.conf
location /public/ {
auth_basic off;
}
But now problem is, that everytime I try to access some file from public folder, say http://mywebsite.com/public/test.html I am keep getting '404 Not Found' If I remove this from my default.conf:
location /public/ {
auth_basic off;
}
I will be able to access http://mywebsite.com/public/test.html but obviously I will have to provide authentication login and password, any idea would be helpful, thank you.
回答1:
Because you don't tell nginx that where to look at documents when location is /public.
Try this:
location /public {
auth_basic off;
root /usr/share/nginx/html;
index index.html index.html;
}
Or, do the better: Move root and index directives inside the server block.
来源:https://stackoverflow.com/questions/55044955/nginx-htpasswd-404-not-found