未找到 NGINX htpasswd 404 [英] NGINX htpasswd 404 not found
问题描述
我正在使用 NGINX 来运行我的网站,我想启用对网站的身份验证,所以我使用了 .htpasswd 文件并且它起作用了,这是我的 default.conf 文件的一部分:
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;
}
它有效,这样我的整个页面都需要身份验证,现在我想让公共"文件夹无需身份验证即可使用,所以我遵循了 官方 NGNIX 文档 并将此指令添加到我的 default.conf
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;
}
但现在的问题是,每次我尝试从公共文件夹访问某个文件时,比如 http://mywebsite.com/public/test.html 如果我从我的 default.conf 中删除它,我会不断收到404 Not Found":
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;
}
我将能够访问 http://mywebsite.com/public/test.html 但显然我必须提供身份验证登录名和密码,任何想法都会有所帮助,谢谢.
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.
推荐答案
因为你没有告诉 nginx 当 location 是/public 时在哪里查看文档.
Because you don't tell nginx that where to look at documents when location is /public.
试试这个:
location /public {
auth_basic off;
root /usr/share/nginx/html;
index index.html index.html;
}
或者,做得更好:在 server 块内移动 root 和 index 指令.
Or, do the better: Move root and index directives inside the server block.
这篇关于未找到 NGINX htpasswd 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!