Authentication Nginx:除索引页外的密码保护文件夹
“我的网站”中的文件夹受密码保护。它包含一个子网站:Authentication Nginx:除索引页外的密码保护文件夹,authentication,nginx,basic-authentication,nginx-location,nginx-config,Authentication,Nginx,Basic Authentication,Nginx Location,Nginx Config,“我的网站”中的文件夹受密码保护。它包含一个子网站:example.com/subsite。我只想公开(删除密码保护)子网站主页,这样任何人都可以浏览到example.com/subsite并获取/subsite/index.html,但文件夹中的任何其他页面仍然需要凭据 身份验证已起作用: location /subsite { auth_basic "Subsite users only."; auth_basic_user_file /etc/nginx/subsite-pwds;
example.com/subsite
。我只想公开(删除密码保护)子网站主页,这样任何人都可以浏览到example.com/subsite
并获取/subsite/index.html
,但文件夹中的任何其他页面仍然需要凭据
身份验证已起作用:
location /subsite {
auth_basic "Subsite users only.";
auth_basic_user_file /etc/nginx/subsite-pwds;
}
我可以公开主页,这需要一些其他资源才能工作
# public access to home page only
location = /subsite/index.html {
auth_basic off;
}
location /subsite/assets {
auth_basic off;
}
location /subsite/img {
auth_basic off;
}
location /subsite/scripts {
auth_basic off;
}
location /subsite/stylesheets {
auth_basic off;
}
这允许任何人获取example.com/subsite/index.html
。但我想要的是任何人都能够获取example.com/subsite
上述第一个位置块的以下替代方案均无效。对example.com/subsite
# 1
location = /subsite {
auth_basic off;
}
# 2
location = /subsite/ {
auth_basic off;
}
# 3
location = /subsite {
auth_basic off;
rewrite ^ /subsite/index.html last;
}
# 4
location ~ ^/subsite$ {
auth_basic off;
}
根据Nginx的默认行为,URI
/subsite
(解析为目录)从外部重定向到/subsite/
索引模块在内部将URI/subsite/
重定向到/subsite/index.html
每个URI,/subsite
、/subsite/
和/subsite/index.html
,都是单独处理的,如果Nginx选择您的位置/子网站{…}
块来处理单个URI,它们中的任何一个都可以触发身份验证检查
要绕过索引页的身份验证检查,除了问题中标识的资源URI之外,还需要一个或多个与三个URI匹配的位置
例如:
location = /subsite {
auth_basic off;
}
location = /subsite/ {
auth_basic off;
}
location = /subsite/index.html {
auth_basic off;
}
location /subsite/assets {
auth_basic off;
}
location /subsite/img {
auth_basic off;
}
location /subsite/scripts {
auth_basic off;
}
location /subsite/stylesheets {
auth_basic off;
}
好主意!但只是尝试了一下,仍然看到了pwd挑战。澄清成功了–非常感谢!你能把这个作为答案贴出来让我接受吗?