Apache 2.4: AuthType Basic and REQUEST_URI - Comparisons (with or without regular expr.) do not work properly
我们在Debian服务器上使用Apache 2.4.10。从充当平衡器的Apache代理服务器(系统和版本相同)重定向请求(当前仅一个平衡成员)。
通常,通过AuthType Basic限制对相关单个虚拟主机的访问。无需身份验证,只能访问包含公共文档的一个文件夹。
我测试了多种方法(新的Apache 2.4语法)来实现这一目标-但是,无论尝试哪种方法,我始终会遇到相同的问题:与REQUEST_URI进行的任何比较都无法按预期进行-不管有没有正则表达式。似乎在进行比较时REQUEST_URI的值无效。
我尝试过以下替代方法:
一种)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <VirtualHost *:80> ServerName domain.name DocumentRoot /var/www/domain.name DirectoryIndex index.php <Directory"/var/www/domain.name/"> AuthType Basic AuthName"Restricted" AuthBasicProvider file AuthUserFile /path/to/user/file <RequireAny> Require method OPTIONS Require expr %{REQUEST_URI} =~ m#^/docs# Require valid-user </RequireAny> Options +ExecCGI +FollowSymLinks AllowOverride All </Directory> CustomLog"/var/log/apache2/test_log""%t REQUEST_URI:%{REQUEST_URI}e" </VirtualHost> |
B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <VirtualHost *:80> ServerName domain.name DocumentRoot /var/www/domain.name DirectoryIndex index.php <Directory"/var/www/domain.name/"> AuthType Basic AuthName"Restricted" AuthBasicProvider file AuthUserFile /path/to/user/file <RequireAny> Require method OPTIONS Require valid-user </RequireAny> Options +ExecCGI +FollowSymLinks AllowOverride All </Directory> <LocationMatch"^/docs"> AuthType None Require all granted </LocationMatch> CustomLog"/var/log/apache2/test_log""%t REQUEST_URI:%{REQUEST_URI}e" </VirtualHost> |
C)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <VirtualHost *:80> ServerName domain.name DocumentRoot /var/www/domain.name DirectoryIndex index.php <Directory"/var/www/domain.name/"> SetEnvIf Request_URI /docs noAuth=1 AuthType Basic AuthName"Restricted Files" AuthBasicProvider file AuthUserFile /path/to/user/file <RequireAny> Require method OPTIONS Require env noauth Require valid-user </RequireAny> Options +ExecCGI +FollowSymLinks AllowOverride All </Directory> CustomLog"/var/log/apache2/test_log""%t REQUEST_URI:%{REQUEST_URI}e" </VirtualHost> |
每种选择似乎都停留在同一问题上。与REQUEST_URI的比较失败或无法正常进行。
一个示例:当我将示例A中的第16行更改为
1 | Require expr %{REQUEST_URI} =~ m#^/[a-z]# |
(作为测试),然后就可以正常工作(无需凭据即可授予访问权限)。
当我将
当我相应地更改示例B中的LocationMatch指令中的正则表达式时,会出现完全相同的行为。
另一个提示:
使用
和:
日志输出始终相同:
如果在没有凭据的情况下授予访问权限,则REQUEST_URI的值与所请求URL的路径部分相同(例如/ docs)。
但是,当出现用户/对话对话框时,该值是一个破折号("-"),这似乎是apache用于空值或不可用值的默认值。
和:
即使我直接访问服务器(没有代理)或使用例如wget向服务器上的localhost发出请求。
有谁知道这是怎么回事!?...
我终于找到了自己的解决方法。 我使用版本A)-但使用环境变量
调整后的A)版本-仅适用于GET请求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <VirtualHost *:80> ServerName domain.name DocumentRoot /var/www/domain.name DirectoryIndex index.php <Directory"/var/www/domain.name/"> AuthType Basic AuthName"Restricted" AuthBasicProvider file AuthUserFile /path/to/user/file <RequireAny> Require method OPTIONS Require expr %{THE_REQUEST} =~ m#GET\\s+\\/docs\\/[^\\/]+\\s+HTTP# Require valid-user </RequireAny> Options +ExecCGI +FollowSymLinks AllowOverride All </Directory> </VirtualHost> |
可以使用其他目录来代替使用位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <VirtualHost *:80> ServerName domain.name DocumentRoot /var/www/domain.name DirectoryIndex index.php <Directory"/var/www/domain.name/"> AuthType Basic AuthName"Restricted" AuthBasicProvider file AuthUserFile /path/to/user/file <RequireAny> Require method OPTIONS Require valid-user </RequireAny> Options +ExecCGI +FollowSymLinks AllowOverride All </Directory> **<Directory"/var/www/domain.name/docs/"> AuthType None Require all granted </Directory>** CustomLog"/var/log/apache2/test_log""%t REQUEST_URI:%{REQUEST_URI}e" </VirtualHost> |
可以通过使用.htaccess来实现相同目的。 如何从子目录中删除.htaccess密码保护,已经回答了一个相关问题