找不到NGINX htpasswd 404

时间:2019-03-07 13:27:23

标签: web nginx nginx-location nginx-config

我正在使用NGINX来运行我的网站,我想启用对网站的身份验证,所以我使用.htpasswd文件进行了工作,并且它可以正常工作,这是我的 default.conf 文件的一部分:< / p>

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 strong>

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,但显然我将必须提供身份验证登录名和密码,任何想法都会有所帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

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

尝试一下:

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

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

相关问题