nginx:auth_basic用于除特定位置之外的所有内容

时间:2012-02-14 17:36:46

标签: nginx

如何针对特定文件的之外的所有内容启用HTTP Basic Auth?

以下是该位置的当前服务器块配置:

location / {
       auth_basic "The password, you must enter.";
       auth_basic_user_file /etc/nginx/htpasswd;
}

location /README {
       auth_basic off;
}

但是,在/README上,它仍然提示输入密码。

我们如何解决这个问题?

谢谢! 标记

4 个答案:

答案 0 :(得分:34)

尝试使用sign =,这可以帮助您:

location = /README {
       auth_basic off;
       allow all; # Allow all to see content 
}

答案 1 :(得分:7)

我正在使用“map”而不是“if”来分配auth_basic领域变量和htpasswd文件:

map $http_host $siteenv {
  default       dev;

  ~^(?<subdomain>.+)\.dev dev;
  ~^(?<subdomain>.+)\.devprofile devprofile;
  ~^(?<subdomain>.+)\.devdebug devdebug;
  ~^(?<subdomain>.+)\.test test;
  ~^(?<subdomain>.+)\.demo demo;
  ~^(?<subdomain>.+)\.stage stage;

  # Live
  ~^(?<subdomain>.+)\.live live;
  ~^.*\.(?P<subdomain>.+)\.[a-zA-Z]* live;
}

map $http_host $auth_type {
  default       "Restricted";

  ~^(?<subdomain>.+)\.dev "Development";
  ~^(?<subdomain>.+)\.devprofile "Development";
  ~^(?<subdomain>.+)\.devdebug "Development";
  ~^(?<subdomain>.+)\.test "Testing";
  ~^(?<subdomain>.+)\.stage "Stage";
  ~^(?<subdomain>.+)\.demo "Demo";

  # Live
  ~^(?<subdomain>.+)\.live "off";
  ~^.*\.(?P<subdomain>.+)\.[a-zA-Z]* "off";
}

server {
  .. etc ..

  auth_basic            $auth_type;
  auth_basic_user_file  /etc/nginx/conf.d/htpasswd-$siteenv;
}

答案 2 :(得分:0)

我正在执行以下操作:

location = /hc.php {
  auth_basic "off";
}

location / {
  try_files $uri $uri/ =404;
}
  • 狭义匹配:location = /somefile.txt {}首先出现,因此location / {}可以捕获其余请求
  • 据我所知,
  • auth_basic "off"要求在其周围加上引号
  • 我还使用完全匹配(如果需要的话),以停止在配置中定义的其他位置上进行迭代(请阅读下面的引用以获取有关其作用的更多信息)

这可能会以不同的顺序工作,并且/或者也没有双引号,但是为什么不尝试尽可能做到正确和完整的事情。

  

最重要的修饰符是:

     

(无)完全没有修饰符意味着该位置被解释为前缀。为了确定匹配项,现在将位置与URI的开头进行匹配。

     

= :如果位置需要与确切的请求URI匹配,则可以使用等号。匹配此修饰符后,搜索将在此处停止。

     

:Tilde表示此位置将被解释为区分大小写的RE匹配。

     

〜* :波浪号后跟一个星号修饰符,表示该位置将作为不区分大小写的RE匹配进行处理。

     

^〜:假设此区块是最佳的非RE匹配,则克拉后面加上波浪号修饰符意味着将不会进行RE匹配。

从这里引用:https://www.keycdn.com/support/nginx-location-directive

答案 3 :(得分:0)

只有auth_basic对我不起作用 如果我们必须在网址下跳过所有uri的身份验证

location ^~ /some/location/to_skip/ {
  auth_basic off;
  try_files $uri $uri/ /index.html;
}      
相关问题