【问题标题】:NGINX htpasswd 404 not foundNGINX htpasswd 404 未找到
【发布时间】:2019-03-07 13:27:23
【问题描述】:

我正在使用 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;
    }

它可以工作,这样我的整个页面都需要身份验证,现在我想让“公共”文件夹在没有身份验证的情况下可用,所以我遵循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】:

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

    试试这个:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-23
      • 2016-12-27
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2018-05-03
      相关资源
      最近更新 更多