问题
不包含 index.html 或 index.php 文件的目录在 Web 浏览器中可见的索引。
我在 Scientific Linux 的 httpd Web 服务器上的配置遇到了很多麻烦,无法停止显示这些索引。
不起作用的配置
httpd.conf 虚拟主机目录指令:
<Directory /home/mydomain.com/htdocs>
Options FollowSymLinks
AllowOverride all
Require all granted
</Directory>
在 .htaccess 中添加以下行:
Options -Indexes
目录索引仍在显示。 .htaccess 设置不起作用!
怎么可能,.htaccess 中的其他设置都可以正常工作,为什么不是这个呢?怎么回事?它应该工作! %#$&^$%@# !!
修复
将 httpd.conf 的 Options 行更改为:
Options +FollowSymLinks
并重新启动网络服务器。
来自 Apache 的核心模组页面:(https://httpd.apache.org/docs/2.4/mod/core.html#options)
将带有 + 或 - 的选项与没有的混合是无效的语法
并且将在服务器启动期间被语法检查拒绝
中止。
不包含 index.html 或 index.php 文件的目录不再显示目录索引。
现在什么!新皱纹
当尝试进行此类目录访问时,新条目开始出现在“error_log”中:
[Fri Aug 19 02:57:39.922872 2016] [autoindex:error] [pid 12479] [client aaa.bbb.ccc.ddd:xxxxx] AH01276: Cannot serve directory /home/mydomain.com/htdocs/dir-without-index-file/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
此条目来自 Apache 模块“autoindex”,其 LogLevel 为“error”,如错误消息的 [autoindex:error] 所示——格式为 [module_name:loglevel]。
要阻止记录这些新条目,需要将 LogLevel 更改为更高级别(例如“crit”)以减少记录——只记录更严重的错误消息。
Apache 2.4 日志级别
请参阅 Apache 2.4 的核心指令以获取 LogLevel。
emerg、alert、crit、error、warn、notice、info、debug、trace1、trace2、trace3、tracr4、trace5、trace6、trace7、trace8
列表中更深的每个级别都会记录任何先前级别的所有消息。
Apache 2.4 的默认级别是“警告”。因此,所有归类为 emerg、alert、crit、error 和 warn 的消息都会写入 error_log。
停止新的 error_log 条目的附加修复
在 httpd.conf 的 .. 部分中添加了以下行:
LogLevel crit
解决方案 1
我的虚拟主机的 httpd.conf .. 配置:
<Directory /home/mydomain.com/htdocs>
Options +FollowSymLinks
AllowOverride all
Require all granted
LogLevel crit
</Directory>
并添加到 /home/mydomain.com/htdocs/.htaccess,即您网站的 .htaccess 文件的根目录:
Options -Indexes
如果您不介意“错误”级别的消息,请省略
LogLevel crit
Scientific Linux - 解决方案 2 - 禁用 mod_autoindex
您的网络空间内不再需要自动索引目录。 .htaccess 没有变化。但是,需要访问/etc/httpd中的httpd配置文件
-
编辑 /etc/httpd/conf.modules.d/00-base.conf 并注释该行:
LoadModule autoindex_module modules/mod_autoindex.so
在它前面添加一个 # 然后保存文件。
-
在目录/etc/httpd/conf.d 重命名(mv)
sudo mv autoindex.conf autoindex.conf.<something_else>
-
重启httpd:
sudo httpd -k restart
或
sudo apachectl restart
autoindex_mod 现在已禁用。
带有 ap2dismod/ap2enmod 命令的 Linux 发行版
禁用自动索引模块输入命令
sudo a2dismod autoindex
启用自动索引模块进入
sudo a2enmod autoindex