nginx使用的是模块ngx_http_autoindex_module来实现的,该模块处理以斜杠(“/“)结尾的请求,并生成目录列表。
nginx编译的时候会自动加载该模块,但是该模块默认是关闭的,使用下来指令来完成对应的配置

autoindex

启用或禁用目录列表输出

语法 autoindex on|off;
默认值 autoindex off;
位置 http、server、location

autoindex_exact_size

对应HTLM格式,指定是否在目录列表展示文件的详细大小
默认为on,显示出文件的确切大小,单位是bytes。改为off后,显示出文件的大概大小,单位是kB或者MB或者GB

语法 autoindex_exact_size on|off;
默认值 autoindex_exact_size on;
位置 http、server、location

autoindex_format

设置目录列表的格式

语法 autoindex_format html|xml|json|jsonp;
默认值 autoindex_format html;
位置 http、server、location

注意:该指令在1.7.9及以后版本中出现

autoindex_localtime

对应HTML格式,是否在目录列表上显示时间。
默认为off,显示的文件时间为GMT时间。改为on后,显示的文件时间为文件的服务器时间

案例

  • 环境文件路径/home/download

image.png

  • Nginx配置

    1. server {
    2. listen 8068;
    3. server_name localhost;
    4. location /download{
    5. root /home;
    6. autoindex on;
    7. autoindex_exact_size on;
    8. autoindex_format html;
    9. autoindex_localtime on;
    10. }
    11. }
  • 访问效果

image.png

  • xml格式

image.png

  • json格式

image.png