导读 文章重点讲述nginx静态服务器搭建
ngx_http_autoindex_module

ngx_http_autoindex_module模块处理以斜杠字符('/')结尾的请求,并生成目录列表。当ngx_http_index_module模块找不到index.html文件时,通常会将请求传递给ngx_http_autoindex_module模块。

配置样例:

location / {
root /home/map/www/; #指定目录所在路径
autoindex on; # 打开目录浏览功能
autoindex_exact_size off;# 以可读的方式显示文件大小,单位为 KB、MB 或者 GB,autoindex_format为html格式时有效
autoindex_localtime on; # 以服务器的文件时间作为显示的时间,autoindex_format为html格式时有效
#autoindex_format html; # 以网页的风格展示目录内容。该属性在1.7.9及以上适用
charset utf-8,gbk; # 展示中文文件名
}
autoindex

开启或者关闭列出目录中文件的功能

Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
autoindex_exact_size

对于HTML格式,指定是否应在目录列表中输出精确的文件大小,或者将其舍入为千字节,兆字节和千兆字节

Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
autoindex_localtime

对于HTML格式,指定是否应该在本地时区或UTC中输出目录列表中的时间。

Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
autoindex_format

设置目录列表的格式。

当使用JSONP格式时,使用callback请求参数设置回调函数的名称。如果参数丢失或具有空值,则使用JSON格式。
XML输出可以使用ngx_http_xslt_module模块进行转换。
Syntax: autoindex_format html | xml | json | jsonp;
Default: autoindex_format html;
Context: http, server, location
This directive appeared in version 1.7.9.
Nginx Fancy Index module

在实际体验中ngx_http_autoindex_module模块显示出的界面有点不好看,所以我们可以采用第三的Nginx Fancy Index module模块,该模块提供动态加载,当然也可以编译,这里采用编译安装,替换原有的nginx二进制文件。

如果是动态加载模块需要在events块上面添加:
load_module "modules/ngx_http_fancyindex_module.so";

# 查看原有的nginx编译模块
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: --prefix=/usr/local/nginx
下载Nginx Fancy Index module 源码包:https://github.com/aperezdc/ngx-fancyindex/archive/v0.4.4.tar.gz

# 编译
./configure --prefix=/usr/local/nginx --add-module=/root/nginx-src/ngx-fancyindex-0.4.4
​
make
#不能make insatll
备份原本的nginx二进制文件,用新生成的二进制文件做替换,在nginx.conf中添加一下内容, fancyindex就已经启用完成

编辑nginx.conf

location / {
#include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf;
root django-document/;
#autoindex on;
#autoindex_exact_size off;
fancyindex on; #开启nginx目录浏览功能
fancyindex_exact_size off; #文件大小从KB开始显示
fancyindex_localtime on; #显示文件修改时间为服务器本地时间
#set $limit_rate 1k;
#root html;
#index index.html index.htm;
}
​

此时主题还是略丑,可以下载主题包 git clone https://github.com/lanffy/Nginx-Fancyindex-Theme.git

复制Nginx-Fancyindex-Theme文件到root 文件夹下,在nginx.conf中引入主题包,美化完毕

location / {
include /usr/local/nginx/html/Nginx-Fancyindex-Theme/fancyindex.conf;
root django-document/;
autoindex on;
autoindex_exact_size off;
}

本文原创地址:https://www.linuxprobe.com/nginx-autoindex-module.html编辑:yangbinbin,审核员:逄增宝