Apache 2.4-如何拒绝对DocumentRoot的访问,但允许对目录索引文件进行“斜杠”访问

问题描述 投票:0回答:1

在我的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/响应。

我该如何纠正这种行为?

apache apache2.4
1个回答
0
投票

遵循提示“我没有明确允许/”,导致被禁止,这使我走上了正确的道路以解决此问题。添加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。

© www.soinside.com 2019 - 2024. All rights reserved.