【问题标题】:Nginx: Password Protect directory index but display subrouteNginx:密码保护目录索引但显示子路由
【发布时间】:2021-12-28 19:54:48
【问题描述】:

我的目录结构如下:

/home
/home/static
/home/static/image1.png
/home/static/pdf1.pdf

我想用密码保护www.mypage.com/uploads 的访问并可视化当前在该目录中的文件的索引,但如果有人转到www.mypage.com/uploads/pdf1.pdf,则不应验证请求并在不询问密码的情况下显示文件。

到目前为止,我有以下 nginx 配置,它要求我在 /uploads 路径以及 /uploads/pdf1.pdf 上输入用户名和密码。

Nginx 配置

location /uploads {
  alias /home/static/;
  autoindex on;
  auth_basic "Private Route";
  auth_basic_user_file /etc/apache2/.htpasswd;
}

[编辑] 建议的工作解决方案:

location ~/uploads$ {
  alias /home/static/;
  autoindex on;
  auth_basic "Private Route";
  auth_basic_user_file /etc/apache2/.htpasswd;
}
location /uploads {
  alias /home/static/;
  autoindex off;
  auth_basic off;
}

【问题讨论】:

    标签: nginx password-protection nginx-config nginx-location


    【解决方案1】:

    可能的解决方案之一:

    location /uploads {
        alias /home/static; # no trailing slash here!
        location = /uploads/ {
            autoindex on;
            auth_basic "Private Route";
            auth_basic_user_file /etc/apache2/.htpasswd;
        }
    }
    

    【讨论】:

    • +1。感谢您的回复。我最终实施了另一个解决方案。不过你的看起来更干净。
    • @renanz 您在编辑中添加的解决方案不可行。它将为您提供 HTTP 403 Forbidden(请求 /uploads 将被重定向到 /uploads/ 禁用自动索引的位置)。此外,您的/uploads$ 正则表达式模式将匹配任何以/uploads 结尾的字符串(例如/some/path/uploads/second/path/uploads 等)。此外,使用正则表达式匹配会对性能产生额外影响,应尽可能避免。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2013-12-05
    • 2015-12-16
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    相关资源
    最近更新 更多