未找到NGINX htpasswd 404

问题描述 投票:0回答:1

我正在使用NGINX运行我的网站,我想对网站启用身份验证,所以我做了,使用.htpasswd文件,它工作,这是我的default.conf文件的一部分:

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;
    }

它工作,这样我的WHOLE页面需要身份验证,现在我想在没有身份验证的情况下使'public'文件夹可用,所以我按照official NGNIX docs并将此指令添加到我的default.conf中

location /public/ {
    auth_basic off;
}

但现在的问题是,每当我尝试从公共文件夹访问某些文件时,请说http://mywebsite.com/public/test.html我一直得到'404 Not Found'如果我从default.conf中删除它:

location /public/ {
    auth_basic off;
}

我将能够访问http://mywebsite.com/public/test.html但显然我将不得不提供身份验证登录名和密码,任何想法都会有所帮助,谢谢。

web nginx nginx-location nginx-config
1个回答
1
投票

因为当位置是/ public时,你不告诉nginx在哪里查看文档。

试试这个:

location /public {
    auth_basic off;
    root   /usr/share/nginx/html;
    index  index.html index.html;
}

或者,做得更好:在服务器块内移动root和index指令。

© www.soinside.com 2019 - 2024. All rights reserved.