Nginx浏览目录配置

发布时间:2020-06-24编辑:佚名阅读(1086)

在项目中有一个功能需要在浏览器页面中浏览服务器的目录。服务器使用Nginx,而Nginx提供了相应的ngx_http_autoindex_module 模块,该模块提供了我们想要的功能。

Nginx ngx_http_autoindex_module 模块

该模块有以下几个命令:

命令默认值值域作用域EG
autoindexoffon:开启目录浏览;
off:关闭目录浏览
http, server, locationautoindex on;打开目录浏览功能
autoindex_formathtmlhtml、xml、json、jsonp 分别用这几个风格展示目录http, server, locationautoindex_format html; 以网页的风格展示目录内容。该属性在1.7.9及以上适用
autoindex_exact_sizeonon:展示文件字节数;
off:以可读的方式显示文件大小
http, server, locationautoindex_exact_size off; 以可读的方式显示文件大小,单位为 KB、MB 或者 GB,autoindex_format为html时有效
autoindex_localtimeoffon、off:是否以服务器的文件时间作为显示的时间http, server, locationautoindex_localtime on; 以服务器的文件时间作为显示的时间,autoindex_format为html时有效

浏览目录基本配置

根据上面的命令,一个简单的Nginx浏览目录的配置如下:

location /download
{
    root /home/map/www/; #指定目录所在路径
    autoindex on; #开启目录浏览
    autoindex_format html; #以html风格将目录展示在浏览器中
    autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
    autoindex_localtime on; #以服务器的文件时间作为显示的时间
}

可以看到页面中的展示信息和配置想要的一致,但还有个问题是中文文件名显示的时候乱码。

中文文件名乱码

要解决上面的问题,只需要添加如下配置即可:

charset utf-8,gbk; #展示中文文件名

完整配置如下:

location /download
{
    root /home/map/www/; #指定目录所在路径
    autoindex on; #开启目录浏览
    autoindex_format html; #以html风格将目录展示在浏览器中
    autoindex_exact_size off; #切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
    autoindex_localtime on; #以服务器的文件时间作为显示的时间
    charset utf-8,gbk; #展示中文文件名
}

文件列表的第一行是一个目录。

  关键字:Nginx浏览目录配置


鼓掌

0

正能量

0

0

呵呵

0


评论区