温馨提示×

怎么禁止目录列表显示

小樊
46
2026-10-11 01:28:35
栏目: 编程语言

禁止目录列表显示(即防止访问某个目录时,服务器把该目录下的文件列出来),不同 Web 服务器配置方式不同,下面是最常见的几种:


一、Apache

1. 修改 .htaccess(推荐,无需重启)

在需要禁止列表的目录下新建或编辑 .htaccess 文件:

Options -Indexes

2. 修改 httpd.conf / apache2.conf

找到对应目录配置:

<Directory "/var/www/html">
    Options -Indexes
    AllowOverride All
</Directory>

修改后重启 Apache:

systemctl restart httpd
# 或
systemctl restart apache2

二、Nginx

Nginx 默认就不显示目录列表,只有显式开启才会。

如果看到了目录列表,说明配置了:

autoindex on;

解决方法:

改为:

autoindex off;

或直接在 location 中删除 autoindex on;

示例:

location /uploads/ {
    autoindex off;
}

重启 Nginx:

nginx -s reload

三、IIS(Windows)

  1. 打开 IIS 管理器
  2. 选择对应网站或目录
  3. 双击 “目录浏览”
  4. 点击 “禁用”

或在 web.config 中:

<configuration>
  <system.webServer>
    <directoryBrowse enabled="false" />
  </system.webServer>
</configuration>

四、Tomcat

在 web.xml 中确保:

<init-param>
    <param-name>listings</param-name>
    <param-value>false</param-value>
</init-param>

五、额外安全建议(很重要)

  • 放一个空 index.html 或 index.php
  • 禁止访问敏感文件(如 .env、.git)
  • 配合权限控制,不要只靠隐藏列表

如果你告诉我用的是哪种服务器(Apache / Nginx / IIS 等)和运行环境,我可以给你更精确的配置示例。

0 踩