Apache服务器运维笔记(5)----容器的处理顺序

  容器在配置文件中是可以多次使用的,同时也可以嵌套使用,但是 Apache 在处理容器时却是有一定顺序的,因此在编写容器配置时需要按照一定的顺序来进行,否则Apache处理的结果很可能不是管理员想要的。

  在Apache中容器的处理顺序如下:

(1) <Directory>与 .htaccess 文件;
(2)<DirectoryMatch> 与 <Directory>
(3)<Files>和<FilesMatch>
(4)<Location>和<LocationMatch>

  Apache会优先处理<Directory>容器(但是不会处理带有正则表达式的<Directory>容器)和 .htaccess 文件,如果 .htaccess 文件与<Directory> 容器有冲突,则以 htaccess 文件的内容来覆盖<Directory>中有冲突的部分。随后Apache 会处理<DirectoryMatch>与<Directory>,再接着处理<Files>和<FilesMatch>容器,最后处理<Location>和<LocationMatch>容器。例如:

<Location /srv/www/html>
Order deny,allow
Allow from all
</Location>

<Directory /srv/www/html>
Order allow,deny
Allow from all
Deny from www.username.com
</Directory>

  在上面的例子中,由于Apache会先处理<Directory  /srv/www/html>容器,最后处理 <Location /srv/www/html> 容器,所以<Location /srv/www/html>容器的内容会覆盖<Directory /srv/www/html>,因此对于禁止 www.username.com 的访问将会被允许所有用户的访问所代替。

  在以上四组容器中,除了<Directory>容器外其他三组容器都会按照在配置文件中的顺序来被处理,而<Directory>容器则按字典顺序由短到长依次被处理,例如:

<Directory /srv/web/html>
Options +Indexes
</Directory>

<Directory /srv/web/>
Options -Indexes
</Directory>

  在上面的例子中,<Directory /srv/web/> 容器虽然排在 <Directory /srv/web/html>容器的后面,但是按照字典顺序由短到长来排列它会第一个被处理。

原文地址:https://www.cnblogs.com/dongling/p/5677055.html