禁止目录浏览(Directory Listing / Directory Browsing)可以防止用户直接通过浏览器查看服务器上的文件列表,提升安全性。不同服务器和场景的配置方式如下:
.htaccess在网站根目录或对应目录下创建/编辑 .htaccess 文件,添加:
Options -Indexes
httpd.conf 或虚拟主机配置<Directory "/var/www/html">
Options -Indexes
</Directory>
修改后重启 Apache:
apachectl restart
# 或
systemctl restart httpd
在 server 或 location 配置中,确保 没有 以下指令:
autoindex on;
应改为:
autoindex off;
示例:
server {
listen 80;
server_name example.com;
root /var/www/html;
location / {
autoindex off;
}
}
重新加载配置:
nginx -s reload
或通过 web.config:
<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>
重启 Tomcat。
express.static 的 index 自动列表即可在浏览器访问一个没有默认首页的目录,例如:
http://example.com/uploads/
若返回 403 Forbidden 或错误页,说明已成功禁止。
如果你能说明使用的是 哪种服务器或运行环境,我可以给出更精确的操作步骤。