在 CentOS 上禁止 Apache 的目录浏览(即禁止访问未设置默认首页的目录时列出文件列表),核心就是关闭 Indexes 选项。下面按常见场景说明。
编辑配置文件(根据系统实际情况选择):
# CentOS 6/7 常见路径
vi /etc/httpd/conf/httpd.conf
# 或虚拟主机
vi /etc/httpd/conf.d/xxx.conf
找到类似内容:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
将 Indexes 删除或改为 -Indexes:
Options -Indexes FollowSymLinks
✅ 含义:
Indexes:开启目录浏览-Indexes:禁止目录浏览<Directory "/var/www/html">
AllowOverride All
</Directory>
.htaccessvi /var/www/html/.htaccess
添加:
Options -Indexes
例如禁止 /var/www/html/upload 目录浏览:
<Directory "/var/www/html/upload">
Options -Indexes
</Directory>
# CentOS 6
service httpd restart
# CentOS 7/8
systemctl restart httpd
访问一个没有默认首页的目录,例如:
http://服务器IP/images/
✅ 成功效果:
403 Forbidden
❌ 失败效果:
文件列表被列出
Options -Indexes -IncludesDirectoryIndex index.php index.html
AllowOverride All如果你用的是 CentOS + SELinux 或 Apache + PHP-FPM,我也可以给你更针对性的配置。