良许Linux教程网 Linux教程 Nginx 静态文件服务器搭建及autoindex模块解析

Nginx 静态文件服务器搭建及autoindex模块解析

ngx_http_autoindex_module
Linux入门
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, locationThis 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 -Vnginx version: nginx/1.14.2built 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.4make#不能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;}

以上就是良许教程网为各位朋友分享的Nginx 静态文件服务器搭建及autoindex模块解析。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你!
公众号

本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部