Press "Enter" to skip to content

nginx使用fancyindex目录索引

一、nginx目录索引

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

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

效果如下:

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

二、使用fancyindex索引

nginx自带的索引功能很单一,界面也很原始。后面有人做了个fancyindex的插件用来强化这个功能,已经被官方采用。官方文档地址https://www.nginx.com/resources/wiki/modules/fancy_index/,fancyindex代码地址:ngx-fancyindex(https://github.com/aperezdc/ngx-fancyindex)。

2.1 编译fancyindex

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

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

解压nginx进入目录,重新执行 configure 命令,指定参数 --add-module 添加fancyindex模块:

./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; # 不显示精确大小
}

其效果如下:

点击表头的 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>

重新载入后的页面:

注:本文转自https://www.dyxmq.cn/other/nginx/nginx-fancyindex.html

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注