禁止目录浏览(Directory Listing / Directory Browsing)可以防止用户直接通过浏览器看到服务器上某个目录下的文件列表,是常见的安全加固手段。不同服务器和场景的设置方式如下:
.htaccess在需要禁止目录浏览的目录中新建或编辑 .htaccess 文件,添加:
Options -Indexes
若想全局生效,可在 httpd.conf 或 apache2.conf 中对应 <Directory> 段设置:
<Directory "/var/www/html">
Options -Indexes
</Directory>
确保没有 Indexes:
Options Indexes FollowSymLinks # 不安全
改为:
Options FollowSymLinks
Nginx 默认不开启目录浏览,只有显式配置才会开启。
autoindex注释或删除以下内容:
autoindex on;
改为:
autoindex off;
示例:
location /uploads/ {
autoindex off;
}
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>
在 conf/web.xml 中,找到 DefaultServlet:
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
(默认通常是 false)
index.html、index.php,避免空目录如果你能告诉我你用的是 哪种服务器(Apache / Nginx / IIS 等)和运行环境,我可以给你更精确的操作步骤。