Nginx目录索引功能简介
为了简便地共享文件,部分用户会选择SVN或FTP服务,但更多人倾向于使用索引(index)功能。Apache的索引功能强大且常见,而Nginx自带的auto_index实现的目录索引功能相对较少,且操作较为简单。首先展示一下最终的效果预览。

Nginx基础索引配置方法
Nginx基础配置方法
location ~ ^/2589(/.*)
{
autoindex on; //开启
autoindex_localtime on;//开启显示功能
}
autoindex指令 语法: autoindex on | off; 默认值: autoindex off; 适用配置段: http, server, location
用于启用或禁用Nginx的目录索引功能。 语法: autoindex_exact_size on | off; 默认值: autoindex_exact_size on; 适用配置段: http, server, location
该参数决定是否需要额外显示文件的精确大小,单位包括字节、MB、GB等。默认情况下此功能是开启的。
syntax: autoindex_localtime on | off;
默认值: autoindex_localtime off; 适用配置段: http, server, location
用于指定是否显示目录或文件的修改时间,默认设置为不显示。
安装FancyIndex美化目录列表
结合Nginx与FancyIndex打造美观的索引目录 Nginx自带的索引目录功能不仅简单而且界面不够美观,若希望制作一个漂亮的索引列表,可以参考以下方案。 安装环境说明 操作系统:CentOS 6.3 Nginx版本:1.4.2 FancyIndex模块:http://wiki.nginx.org/NgxFancyIndex 下载安装FancyIndex 对比Nginx内置的Index效果(参考前文图片),具体差异如下所示。
# wget http://gitorious.org/ngx-fancyindex/ngx-fancyindex/archive-tarball/master
# tar -xzvf master
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=../ngx-fancyindex-ngx-fancyindex
# make
# make install
FancyIndex核心参数详解
FancyIndex索引配置示例
server {
listen 80;
server_name test.jb51.net;
access_log /data/logs/nginx/test.jb51.net.access.log main;
index index.html index.php index.html;
root /data/site/test.jb51.net;
location / {
}
location ~ ^/2589(/.*)
{
fancyindex on;
fancyindex_exact_size off;
fancyindex_localtime on;
fancyindex_footer "myfooter.shtml";
}
}
查看Nginx加载FancyIndex模块后的实际效果,如下图所示。

相比自带样式的美观程度提升难以量化,总之视觉效果得到了显著改善。
关键参数解释: fancyindex on:开启Fancy索引功能 fancyindex_exact_size off:不显示精确文件大小,采用四舍五入方式,例如1.9M会显示为2M。若开启,则单位为字节 fancyindex_localtime on:使用服务器本地时间显示 fancyindex_footer "myfooter.shtml":将当前路径下的myfooter.shtml内容作为页面底部显示。若文件不存在,底部将显示404错误 myfooter.shtml的具体内容如下:
回到顶部
WordPress
版权所有 © 2006-2026 本站
关于我们 | 网站导航 | 网站地图 |苏ICP备14036222号
fancy指令使用:
fancyindex 语法:
*fancyindex* [*on* | *off*]
默认值: fancyindex off 配置块: http, server, location 描述: 用于开启或关闭目录索引功能 fancyindex_css_href 语法:
*fancyindex_css_href uri*
默认值: fancyindex_css_href "" 配置块: http, server, location 描述: 指定外部CSS文件的路径,该CSS将替换现有的默认样式。如果具备CSS知识,可以进一步美化索引列表。由于本站缺乏专业网页设计师,此处仅使用默认样式^^ fancyindex_exact_size 语法:
*fancyindex_exact_size* [*on* | *off*]
默认值: fancyindex_exact_size on 配置块: http, server, location 描述: 定义文件大小的显示方式。默认为on,表示使用精确值,单位为字节;设为off时,单位为KB、MB、GB,若含小数点则进行四舍五入。例如1.9MB将显示为2MB。
*fancyindex_footer path*
默认值: fancyindex_footer "" 配置块: http, server, location 描述: 指定嵌入到索引页面底部的文件路径,具体效果请参阅本文首张图片。
*fancyindex_header path*
默认值: fancyindex_header "" 配置块: http, server, location 描述: 指定嵌入到索引页面头部的文件路径。用法与fancyindex_footer类似。
*fancyindex_ignore string1 [string2 [... stringN]]*
默认值: No default. 配置块: http, server, location 描述: 设置需要隐藏的文件或目录。如果Nginx支持正则表达式,可以使用正则来过滤。 例如,若想隐藏以dir开头的文件或目录以及filea.txt文件,配置如下:
fancyindex_ignore "dir*" "filea.txt"
默认值: fancyindex_localtime off 配置块: http, server, location Description: 使用当地时间显示文件的创建时间,默认值为off(即显示GMT时间)。
*fancyindex_localtime* [*on* | *off*]
默认值: fancyindex_localtime off 配置块: http, server, location Description: 使用当地时间显示文件的创建时间,默认是off(GMT时间)