Nginx 配置目录文件列表显示功能

发布时间: 更新时间: 总字数:390 阅读时间:1m 作者:IP:上海 网址

打开 nginx 的目录功能,可以使 nginx 像下载服务器那样提供文件下载功能。

开启方法

官方地址:https://nginx.org/en/docs/http/ngx_http_autoindex_module.html

正常访问 nginx 的话是显示 nginx 欢迎页,也就是/nginx/html/index.html 文件;如果要显示/html/目录下所有的文件,需要打开目录文件列表显示;在 nginx.conf 主配置文件中 http 或 location 代码段中,配置一段代码即可实现:

代码如下:

location / {
    alias   /some-dir;
    # default_type  application/octet-stream;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    # add_header Content-Type text/html;
    # add_before_body /autoindex/header.html;
    # add_after_body /autoindex/footer.html;
}

参数说明:

  • autoindex on; 自动显示目录
  • autoindex_exact_size off; 默认为 on,显示出文件的确切大小,单位是 bytes
    • 改为 off 后,显示出文件的大概大小,单位是 kB 或者 MB 或者 GB
  • autoindex_localtime on; 默认为 off,显示的文件时间为 GMT 时间
    • 改为 on 后,显示的文件时间为文件的服务器时间
  • 扩展
    • add_header、add_before_body、add_after_body 可以定制文件列表