简体   繁体   中英

NGINX htpasswd 404 not found

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM