Apache - 如何拒绝目录但允许该目录中的一个文件

问题描述 投票:23回答:5

我尝试配置我的Apache .conf文件以拒绝某个类别的列表,但我想允许此类别中的特定文件。看起来目录规则比“文件”规则“更强”,所以当使用两者时 - 我无法访问该特定文件。

这是我尝试的:

<Directory /var/www/denied_directory>
     order deny,allow
     Deny From All
</Directory>

<Files safefile.php>
    Order Allow,Deny
    Allow from All
</Files>
security apache webserver
5个回答
20
投票

如果配置正确,它可以正常工作:

   <Directory /var/www/denied_directory>
        Order allow,deny
        <Files test.php>
           Order deny,allow
        </Files>
   </Directory>

9
投票

在Apache 2.4中,对环境变量进行额外测试以获得良好的衡量标准:

另见:Require Directive

<Directory "/wikis/foswiki">

    Require all denied

    # Allow access to toplevel files ending in .html (in particular index.html) only 
    # (comment out if you don't care for this)

    <Files ~ "\.html$">

       <RequireAll>
          Require all granted
          Require not env blockAccess
       </RequireAll>

    </Files>

</Directory>

4
投票

将您的files指令放在目录指令中。


1
投票

@ acond的答案中有一条缺失的线。我认为它需要Allow

<Directory /var/www/denied_directory>
     order deny,allow
     Deny from All
    <Files safefile.php>
        order deny,allow
        Allow from All
    </Files>
</Directory>

由于每个指令中只有一条规则,我怀疑order线可能无关紧要。虽然可能是最外面的一个,但因为嵌套了多个规则。 (我是apache配置的新手)


0
投票

在访问受HTTP密码限制时允许特定文件。请注意,密码保护是在文件系统的基础上定义的,特定的允许文件由URI定义。针对Apache 2.4进行了更新。

<Directory /path/to/directory/>
    AuthName SecureArea
    AuthType Basic
    AuthUserFile /path/to/passwd-file
    Require user my-user

    SetEnvIf Request_URI "path/to/uri-allowed-1.php" allowedURL
    SetEnvIf Request_URI "path/to/uri-allowed-2.php" allowedURL
    Require env allowedURL
</Directory>
© www.soinside.com 2019 - 2024. All rights reserved.