在我的Apache 2.4 VirtualHost配置中,默认情况下,我想拒绝访问DocumentRoot
中所有未明确启用的内容。为此,我写了:
DocumentRoot /var/www
<Directory "/var/www">
Require all denied
<Files "index.html">
Require all granted
</Files>
</Directory>
这将允许直接访问http://myserver.example/index.html
,但是对于间接访问403
会导致http://myserver.example/
响应。
我该如何纠正这种行为?
遵循提示“我没有明确允许/
”,导致被禁止,这使我走上了正确的道路以解决此问题。添加LocationMatch
指令专门处理尾部斜杠会导致所需的行为:
DocumentRoot /var/www
<Directory "/var/www/">
Require all denied
<Files "index.html">
Require all granted
</Files>
</Directory>
# Regex anchored at string beginning and end
# -> only matches "/"
<LocationMatch "^/$">
Require all granted
</LocationMatch>
请注意,添加<Files "/">
指令不起作用,可能是因为所访问的资源实际上不是文件。<Location />
都不正确,因为它将应用于整个VirtualHost。