# Web 服务器目录索引

Web 服务器目录索引是客户端请求一个目录（即以 `/` 或 `/projects/` 这类路径结尾的 URL）而非特定文件时，Web 服务器返回的响应。服务器要么返回该目录中指定的索引文件（通常是 `index.html`），要么在不存在索引文件且启用了目录列表功能时，生成该目录内容的列表。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup><sup> • </sup><sup>[2](https://datatracker.ietf.org/doc/rfc2055/)</sup>

| 关键事实 | 详情 |
|---|---|
| 两种响应模式 | 返回配置好的索引文件；或在无索引文件且启用列表功能时生成目录列表<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup> |
| Apache 控制方式 | 索引文件由 `DirectoryIndex`（mod_dir）设置；生成的列表来自 mod_autoindex，通过 `Options +Indexes` 启用<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup> |
| nginx 控制方式 | `ngx_http_index_module`（默认 `index index.html;`）处理索引文件；`ngx_http_autoindex_module`（`autoindex on;`，默认关闭）生成列表<sup>[3](https://nginx.org/en/docs/http/ngx_http_index_module.html)</sup><sup> • </sup><sup>[4](https://nginx.org/en/docs/http/ngx_http_autoindex_module.html)</sup> |
| IIS 控制方式 | Web.config 中的 `<directoryBrowse enabled="true">`；`enabled` 属性默认为 `false`<sup>[5](https://learn.microsoft.com/en-us/iis/configuration/system.webserver/directorybrowse)</sup> |
| Python 行为 | `SimpleHTTPRequestHandler` 先检查 `index.html`，再检查 `index.htm`，否则通过 `list_directory()` 生成列表<sup>[6](https://docs.python.org/3.11/library/http.server.html)</sup> |
| 安全默认值 | 安全指南将目录列表视为信息泄露；NIST 2008 年关于安全 Web 服务器的公告将“禁用目录列表”列为加固步骤之一<sup>[7](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/04-Review_Old_Backup_and_Unreferenced_Files_for_Sensitive_Information/)</sup><sup> • </sup><sup>[8](https://csrc.nist.gov/files/pubs/shared/itlb/itlbul2008-01.pdf)</sup> |

## 两种模式的工作方式

当浏览器请求一个目录时，服务器首先在该目录中查找索引文件。RFC 2055——WebNFS 服务器规范——描述了 1996 年时的这一惯例：大多数 HTTP 服务器支持索引文件的概念，如果浏览器引用的目录中包含索引文件，服务器返回的是索引文件的内容而非目录列表；索引文件通常称为 `index.html`。<sup>[2](https://datatracker.ietf.org/doc/rfc2055/)</sup> MDN 的 Web 开发指南从作者角度描述了同样的行为：未指定文件名时，Web 服务器会查找名为 `index.html` 的默认着陆页，因此指向 `https://www.example.com/projects/` 的链接会到达 `projects/index.html`，而文件名不会出现在 URL 中。<sup>[9](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content/Creating_links)</sup>

如果不存在索引文件，服务器的行为取决于配置。它可能返回错误，也可能生成该目录下文件和子目录的列表。在 [Apache HTTP Server](https://www.edgepedia.org/zh/apache-http-server) 中，这两种功能被刻意分离：`mod_dir` 提供“尾部斜杠”重定向和目录索引文件的返回，而 `mod_autoindex` 生成自动列表（类似于 Unix 的 `ls` 命令），可以独立移除或替换。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup><sup> • </sup><sup>[10](https://d.apache.org/docs/2.4/en/mod/)</sup>

## Apache HTTP Server

**索引文件。** `DirectoryIndex` 指令设置用作索引的文件名，由 `mod_dir` 控制。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup> Apache 的性能文档建议列出一组明确、完整的候选文件，并将最常用的放在最前面，例如 `DirectoryIndex index.cgi index.pl index.shtml index.html`，而不是使用 `DirectoryIndex index` 这样的通配形式，因为每个候选文件都意味着每次请求多一次文件系统检查。<sup>[11](https://d.apache.org/docs/2.4/en/misc/perf-tuning.html)</sup>

**生成的列表。** 自动索引生成通过 `Options +Indexes` 启用，可以用 `<Directory>` 段限定到某个目录树。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup><sup> • </sup><sup>[12](https://d.apache.org/docs/2.4/sections.html)</sup> 配合 `IndexOptions` 指令的 `FancyIndexing` 选项，列表会变成格式化的表格，其列标题是链接：点击某个标题会重新生成按该列排序的列表，再次点击同一标题则在升序和降序之间切换。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup>

客户端还可以通过目录 URL 上的查询字符串参数控制列表：`C=N`、`C=M`、`C=S` 和 `C=D` 分别按名称、最后修改日期、大小或描述排序；`O=A` 和 `O=D` 选择升序或降序；`F=0`、`F=1` 和 `F=2` 选择纯文本、花式索引或 HTML 表格格式；`V=0` 和 `V=1` 切换版本排序；`P=模式` 将列表限制为匹配的文件。`IndexOptions IgnoreClient` 选项会禁用所有这些客户端控制。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup>

其他 `IndexOptions` 设置影响显示效果：`FoldersFirst` 将子目录列在文件之前，`NameWidth` 以字节为单位设置文件名列宽（默认 20，最小值大于 5，或设为 `*` 表示自动宽度），`ScanHTMLTitles` 以 CPU 和磁盘开销为代价提取 HTML `title` 元素作为描述，`SuppressColumnSorting` 移除可排序的标题链接。<sup>[1](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)</sup>

## nginx

nginx 将同样的工作拆分到多个模块中。`ngx_http_index_module` 处理以斜杠结尾的请求，并通过 `index` 指令定义索引文件，其默认值为 `index index.html;`。文件名可以包含变量，文件按指定顺序检查，列表的最后一个元素可以是绝对路径。使用索引文件会触发内部重定向，因此对 `/` 的请求可以在不同的 location 块中作为 `/index.html` 处理。<sup>[3](https://nginx.org/en/docs/http/ngx_http_index_module.html)</sup>

当索引模块找不到索引文件时，请求通常被传递给 `ngx_http_autoindex_module`，它在设置了 `autoindex on;` 时生成目录列表（默认为 `off`）。其指令控制输出：`autoindex_exact_size` 在精确文件大小与按千字节、兆字节、吉字节取整的大小之间选择；`autoindex_localtime` 选择时间戳使用本地时间还是 UTC；`autoindex_format` 选择 `html`、`xml`、`json` 或 `jsonp` 输出，该指令在 nginx 1.7.9 版本中引入。XML 输出可以用 `ngx_http_xslt_module` 进行转换。<sup>[4](https://nginx.org/en/docs/http/ngx_http_autoindex_module.html)</sup> 第三个模块 `ngx_http_random_index_module` 也可以处理以斜杠结尾的请求。<sup>[3](https://nginx.org/en/docs/http/ngx_http_index_module.html)</sup> 自动索引模块可以在编译时通过 `--without-http_autoindex_module` 排除。<sup>[13](https://nginx.org/en/docs/configure.html)</sup>

## IIS 及其他服务器

在 [Internet Information Services](https://www.edgepedia.org/zh/internet-information-services) 中，对应的功能是目录浏览，由 Web.config 中的 `<directoryBrowse>` 元素在站点、应用程序或目录级别控制。`enabled` 属性默认为 `false`。`showFlags` 属性选择列表中每一行显示的内容；其默认值为 `Date`、`Time`、`Size` 和 `Extension`，设为 `None` 则只返回文件和目录名称。<sup>[5](https://learn.microsoft.com/en-us/iis/configuration/system.webserver/directorybrowse)</sup>

IIS 的默认文档机制是索引文件的对应物。对于未指定文档名称的请求，IIS 获取配置的默认文档列表，并在内容路径中逐一检查，直到找到第一个匹配项；微软的托管指南指出，配置较少的默认文档可以加快请求速度，因为每次未命中都意味着又一次查找。<sup>[14](https://learn.microsoft.com/en-us/iis/web-hosting/web-server-for-shared-hosting/default-documents)</sup> 当两种机制都不适用时，IIS 返回 HTTP 错误 403.14“禁止访问”，描述为 Web 服务器被配置为不列出目录内容；文档给出的修复方法是启用目录浏览，或添加并启用站点根文件夹中存在的默认文档。<sup>[15](https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/iis/site-behavior-performance/http-403-14-forbidden-webpage)</sup>

Python 内置的 `http.server` 模块以最小形式展示了同样的逻辑。`SimpleHTTPRequestHandler` 将请求映射为相对于当前目录的路径；如果请求映射到目录，它按顺序检查 `index.html` 或 `index.htm`，找到则返回文件内容，否则调用 `list_directory()` 生成列表，该函数使用 `os.listdir()`，如果列表生成失败则返回 404。<sup>[6](https://docs.python.org/3.11/library/http.server.html)</sup> Node.js 的 `http-server` 工具行为类似：存在 `index.html` 时返回它，不存在时显示目录。<sup>[16](https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Tools_and_setup/set_up_a_local_testing_server)</sup>

## 安全考量

生成的目录索引会向任何能够请求该目录的人暴露服务器的文件结构。OWASP Web 安全测试指南称目录列表是配置不当的服务器泄露未被引用页面的最明显方式，并指示测试者请求所有枚举的目录以识别提供列表的目录。<sup>[7](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/04-Review_Old_Backup_and_Unreferenced_Files_for_Sensitive_Information/)</sup> 风险会与其他遗留内容叠加：诸如 `viewdoc.bak` 之类的备份文件可能返回服务器端源代码而非执行结果，旧版本可能保留当前文件已修复的漏洞，未被引用的配置文件或日志文件可能泄露凭据和路径。<sup>[7](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/04-Review_Old_Backup_and_Unreferenced_Files_for_Sensitive_Information/)</sup> 搜索引擎可以索引可浏览的目录，因此从站点移除的内容可能仍存在于搜索缓存中。<sup>[7](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/04-Review_Old_Backup_and_Unreferenced_Files_for_Sensitive_Information/)</sup>

因此，主流服务器默认关闭列表功能：nginx 的 `autoindex` 默认为 `off`<sup>[4](https://nginx.org/en/docs/http/ngx_http_autoindex_module.html)</sup>，IIS 的 `directoryBrowse` 默认为 `false`<sup>[5](https://learn.microsoft.com/en-us/iis/configuration/system.webserver/directorybrowse)</sup>，NIST 关于保护面向公众的 Web 服务器的指南将禁用目录列表列入其配置建议之中。<sup>[8](https://csrc.nist.gov/files/pubs/shared/itlb/itlbul2008-01.pdf)</sup> Nessus 和 Nikto2 等漏洞扫描器包含检查项，会报告任何允许索引的 Web 目录，OWASP 指南还建议默认禁用云对象存储上的公开列表功能。<sup>[7](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/04-Review_Old_Backup_and_Unreferenced_Files_for_Sensitive_Information/)</sup>

## References

1. [Apache Module mod_autoindex, Apache HTTP Server Version 2.4 documentation](https://d.apache.org/docs/2.4/en/mod/mod_autoindex.html)
2. [RFC 2055: WebNFS Server Specification, IETF](https://datatracker.ietf.org/doc/rfc2055/)
3. [Module ngx_http_index_module, nginx documentation](https://nginx.org/en/docs/http/ngx_http_index_module.html)
4. [Module ngx_http_autoindex_module, nginx documentation](https://nginx.org/en/docs/http/ngx_http_autoindex_module.html)
5. [Directory Browse `<directoryBrowse>`, Microsoft Learn, IIS configuration](https://learn.microsoft.com/en-us/iis/configuration/system.webserver/directorybrowse)
6. [http.server, Python 3.11.17 documentation](https://docs.python.org/3.11/library/http.server.html)
7. [WSTG v4.2: Review Old Backup and Unreferenced Files for Sensitive Information, OWASP Web Security Testing Guide](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/02-Configuration_and_Deployment_Management_Testing/04-Review_Old_Backup_and_Unreferenced_Files_for_Sensitive_Information/)
8. [ITL Bulletin: Secure Web Servers, January 2008, NIST](https://csrc.nist.gov/files/pubs/shared/itlb/itlbul2008-01.pdf)
9. [Creating links, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content/Creating_links)
10. [Module Index, Apache HTTP Server Version 2.4 documentation](https://d.apache.org/docs/2.4/en/mod/)
11. [Apache Performance Tuning, Apache HTTP Server Version 2.4 documentation](https://d.apache.org/docs/2.4/en/misc/perf-tuning.html)
12. [Configuration Sections, Apache HTTP Server Version 2.4 documentation](https://d.apache.org/docs/2.4/sections.html)
13. [Building nginx from Sources, nginx documentation](https://nginx.org/en/docs/configure.html)
14. [Default Documents, Microsoft Learn, IIS web hosting](https://learn.microsoft.com/en-us/iis/web-hosting/web-server-for-shared-hosting/default-documents)
15. [HTTP Error 403.14 when You Open IIS Webpages, Microsoft Learn](https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/iis/site-behavior-performance/http-403-14-forbidden-webpage)
16. [How do you set up a local testing server?, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Tools_and_setup/set_up_a_local_testing_server)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/web-server-directory-index

English original: https://www.edgepedia.org/web-server-directory-index
