【问题标题】:Apache 2.4 - How to deny access to DocumentRoot but allow 'trailing slash' access to DirectoryIndex fileApache 2.4 - 如何拒绝访问 DocumentRoot 但允许“斜杠”访问 DirectoryIndex 文件
【发布时间】:2020-03-19 14:27:56
【问题描述】:

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

我该如何纠正这种行为?

【问题讨论】:

  • 它的行为似乎完全如你所愿:你没有明确允许/,所以它是被禁止的
  • @Dusan :谢谢,这让我走上了正轨,请看答案!原因我还不清楚,但我至少找到了一个切实可行的解决方案。

标签: apache apache2.4


【解决方案1】:

根据我“没有明确允许/”的提示,导致它被禁止,让我走上了解决这个问题的正确轨道。 添加一个专门处理尾部斜杠的 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>

请注意,添加&lt;Files "/"&gt; 指令不起作用,可能是因为访问的资源不是真正的文件。 &lt;Location /&gt; 也不是正确的,因为它将应用于整个 VirtualHost。

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2016-03-06
    • 2014-12-31
    • 1970-01-01
    • 2012-12-27
    • 2011-12-12
    • 2013-09-22
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多