禁止目录列表显示(即防止访问某个目录时,服务器把该目录下的文件列出来),不同 Web 服务器配置方式不同,下面是最常见的几种:
.htaccess(推荐,无需重启)在需要禁止列表的目录下新建或编辑 .htaccess 文件:
Options -Indexes
httpd.conf / apache2.conf找到对应目录配置:
<Directory "/var/www/html">
Options -Indexes
AllowOverride All
</Directory>
修改后重启 Apache:
systemctl restart httpd
# 或
systemctl restart apache2
Nginx 默认就不显示目录列表,只有显式开启才会。
autoindex on;
改为:
autoindex off;
或直接在 location 中删除 autoindex on;
示例:
location /uploads/ {
autoindex off;
}
重启 Nginx:
nginx -s reload
或在 web.config 中:
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>
在 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 等)和运行环境,我可以给你更精确的配置示例。