一、nginx目录索引

nginx中内置了目录索引命令 auto_index ,十分方便就能给目录生成web索引:

location /ftp/ {
    alias /data/html;
    autoindex on;
}

效果如下:

PIC20180902_214954.png

两个可选的命令是 autoindex_exact_sizeautoindex_localtime ,分别表示是否精确显示文件大小(以字节方式)和是否显示本地时间,两个都 不开启 的情况下是这样的:

PIC20180902_215504.png

二、使用fancyindex索引

nginx自带的索引功能很单一,界面也很原始。后面有人做了个fancyindex的插件用来强化这个功能,已经被官方采用。官方文档地址,fancyindex代码地址:ngx-fancyindex

2.1 编译fancyindex

安装fancyindex要重新编译nginx,首先下载fancyindex源码

git clone https://github.com/aperezdc/ngx-fancyindex

解压nginx进入目录,重新执行 configure 命令,指定参数 --add-module 添加fancyindex模块,nginx安装的具体流程可以参考源码编译安装nginx

./configure --user=www --group=www \
    --prefix=/usr/local/nginx-1.12.2 \
    --with-http_stub_status_module \
    --with-http_ssl_module \
    --add-module=../ngx-fancyindex # 添加fancyindex模块

执行make和make install即可完成安装。

2.2 使用fancyindex

fancyindex的指令:

location /ftp/ {
    alias /data/software/nginx/;
    fancyindex on; # 使用fancyindex
    fancyindex_exact_size off; # 不显示精确大小
}

其效果如下:

fancyindex.png

点击表头的 File NameFile Size 或者 Date 能对文件进行排序。

2.3 其他用法

fancyindex提供了自定义页头和页脚,分别通过指令 fancyindex_headerfancyindex_footer 完成。

例如在页脚加上一个超链接到百度首页:

location /ftp/ {
    alias /data/software/nginx/;
    fancyindex on;
    fancyindex_exact_size off;
    fancyindex_footer "fancy_footer.html";
}

然后在 网站根目录下 加上一个 fancy_footer.html

<div id="footer">
    <a href="https://www.baidu.com">百度一下<a>
</div>

重新载入后的页面:

PIC20180902_221620.png

最后修改:2018 年 12 月 16 日
如果觉得我的文章对你有用,请随意赞赏