Nginx Fancy Index module

Nginx上有个小服务,每天都会产生日志,时间久了要拉到最下面才能看到最新的结果,想着能不能按时间排序下,这样看着方便,于是找到了Fancy Index module

Fancy Index

与内置的autoindex模块一样,Fancy Index模块允许对生成的内容进行一定程序的定制,给文件列表增加了样式感。

因为在之前部署过Nginx,直接用以前下载的源文件即可,新建一个目录单独存放下载的Nginx插件:

1
2
3
$ mkdir ngx_module
$ cd ngx_module
$ git clone https://github.com/aperezdc/ngx-fancyindex.git

进入Nginx源文件中添加插件模块进行编译:

1
2
3
$ cd nginx-1.16.1/
$ ./configure --prefix=/usr/local/nginx --add-module=../ngx_module/ngx-fancyindex
$ make

如果是初次安装,直接执行下面的命令即可。

1
$ make install

我在之前有安装过,先对Nginx可执行文件进行备份,再将新编译好的文件替换进去:

1
2
$ cp sbin/nginx sbin/nginx.old
$ cp -f objs/nginx ../nginx/sbin/nginx

编辑配置文件,在自己需要的location处修改配置为:

1
2
3
4
5
6
7
8
location / {
alias dlib/;
# autoindex on;
fancyindex on;
fancyindex_exact_size off;
fancyindex_localtime on;
fancyindex_default_sort date_desc;
}

Fancy Index项目文档中有关于指令fancyindex_default_sort的使用描述,可以根据文件名、大小、时间进行升/降排序。其他字段与autoindex的用法基本一致。

现在停掉现在的Nginx服务之后重启:

1
$ nginx -s stop && nginx

此时访问文件列表可以看出已经变更为按时间排序了。

0%