我试图通过Apache暴露一个位置。 通常情况下,我的虚拟主机中有这个块
<Location /> AuthType Basic AuthUserFile /web/.htpasswd AuthName "Test Site" Require valid-user </Location>
这工作得很好 – 一切服务需要一个有效的用户。 现在我想公开一个不需要身份validation的服务,所以我正在寻找一种方法来使除/服务之外的所有位置都需要身份validation。 我一直在玩LocationMatch,但是我不清楚它在做什么。
<LocationMatch ^/(?!services)[^.]*$> AuthType Basic ... </LocationMatch>
允许/服务及其下的所有内容跳过LocationMatch,但是它具有允许example.com/.somefile绕过LocationMatch块的副作用。
另外,当我尝试
<LocationMatch ^/(?!services)> AuthType Basic ... </LocationMatch>
一切(包括/服务)都由LocationMatch匹配。
我会很感激,如果有人能告诉我什么[^。*]类做的第二个testing没有,以及如何只暴露/服务,同时保持所有其他path进行身份validation。
那么, [^.]意思是“不是一个”,这就是为什么/ /.somefile不匹配。 最后一个例子不起作用的一个可能的原因是因为Perl兼容的正则expression式只支持从Apache 2.0开始,所以如果你在Apache 1.3上(你真的应该在你的问题中指定一个Apache版本),那么是的。
Antonio Lorusso的这个页面build议如下从apacheauthentication中排除文件夹:
<Location "/"> AuthType Basic AuthName "Restricted Files" AuthUserFile /var/www/clients/client12/web17/passwd AuthGroupFile /dev/null Require valid-user SetEnvIf Request_URI "^/(admin|skin|js|index)(.*)$" allow SetEnvIf Request_URI "^/favicon.ico$" allow Order allow,deny Allow from env=allow Satisfy Any </Location>
在这种情况下,以/ admin,/ skin,/ js或/ index开头的URL将被auth忽略。
本节的关键部分是:
SetEnvIf Request_URI "^/(admin|skin|js|index)(.*)$" allow
在你的情况下,适当的代码将是:
SetEnvIf Request_URI "^/services(.*)$" allow
_
Mantain的
<Location /> AuthType Basic AuthUserFile /web/.htpasswd AuthName "Test Site" Require valid-user </Location>
并添加一个这个新的位置
<Location /services> Satisfy any Order deny,allow </Location>
使用apache 2.4,你可以这样做:
<Location /> <If "%{REQUEST_URI} =~ m#^/services/#"> Satisfy any Order deny,allow </If> <Else> AuthType Basic AuthUserFile /web/.htpasswd AuthName "Test Site" Require valid-user </Else> </Location>
尝试了许多方法,没有成功。
最后这个工作在Apache 2.2上。
Hère是LocationMatch指令的用途
<LocationMatch /(firstpath|secondpath).*> AuthType Basic AuthName "Restricted AREA " AuthUserFile /etc/apache2/security/password AuthGroupFile /etc/apache2/security/group Require group admin Order allow,deny Allow from 127.0.0.1 Allow from 10.0.0.0/24 Allow from env=allowclient Satisfy any </LocationMatch>