# Nginx教程 - 13 目录浏览功能

目录浏览功能是一种Web服务器功能,它允许用户以列表的方式,查看服务器上指定目录下的文件内容。这种功能在很多Web服务器软件中都可以配置,包括Apache和Nginx。

目录浏览功能一般用来共享文件,允许用户浏览和下载各种文件。

# 13.1 开启目录浏览功能

在站点的 server 模块下添加如下配置,开启目录浏览功能:

server {
    # ...其他配置
    
    autoindex on;  # 开启目录浏览功能
    autoindex_exact_size off;  #显示文件大小的时候,显示单位

    # ...其他配置
}
1
2
3
4
5
6
7
8

需要注意,开启目录浏览功能,站点根目录下不能存在 index.html文件,否则会访问到首页。


访问效果:

通过浏览器访问站点的时候,显示如下:

# 13.2 针对指定的目录开启目录浏览功能

上面是针对站点根目录开启目录浏览功能,还可以只针对某个目录开启目录浏览功能:

server {
    listen       80;  # 监听80端口
    server_name  localhost; 
    
    location / {
        root   /home/doubi/html;  # 站点路径
        index  index.html;
    }

    location /test {
        root   /home/doubi/html;  
        autoindex on;  # 开启目录浏览功能
        autoindex_exact_size off;  #显示文件大小的时候,显示单位
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

同样,test 目录下也不能有 index.html 文件。


访问效果: