安装NGINX花式索引模块
要将Fancy Index模块安装在已经构建的NGINX安装上,首先需要知道正在运行哪个NGINX版本。通过运行 nginx -v 命令进行检查
[root@i ~] nginx -v nginx version: nginx/1.19.4nginx -v
根据如上的获取到的版本,下载相应的安装源并解压
wget https://nginx.org/download/nginx-1.19.4.tar.gz tar -xzvf nginx-1.19.4.tar.gz
使用 git 命令下载花式索引模块
git clone https://github.com/aperezdc/ngx-fancyindex.git
注意
为了动态加载模块,需要使用与 Nginx 配置完全相同的参数,否则该模块将无法正确编译,并且很可能导致以下错误:
nginx: [emerg] module is not binary compatible in /etc/nginx/nginx.conf
获取当前NGINX配置参数
[root@i ~] nginx -V configure arguments: --prefix=/etc/nginx ...
安装依赖包
dnf -y install gcc redhat-rpm-config pcre-devel openssl-devel libxslt-devel gd gd-devel perl-ExtUtils-Embed gperftools-devel
如上将输出与之相关的所有 configure arguments,现在编译Fancy Index模块。运行以下命令,替换[args]为上面找到的参数:
cd nginx-1.19.4 ./configure [args] --add-dynamic-module=../ngx-fancyindex make make install
将 fancyindex 配置的nginx
location /test { fancyindex on; # Enable fancy indexes. fancyindex_exact_size off; # Output human-readable file sizes. }
测试
如下通过后,打开:https://example.com/test 即可看到结果
[root@i ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
主题载入的三种方式
方案壹(推荐)
索引目录与主题目录分开,主题目录使用别名(alias)绑定到其它路径,不置入索引目录。如下,索引对外显示的路径是相对于网站根的下一级(/one),而主题资源包是存放在:/etc/nginx/fancyindex_theme,其资源引入路径则为:/one_theme/theme-resource-packages/js/js.js
location /one { fancyindex on; fancyindex_localtime on; fancyindex_exact_size off; fancyindex_name_length 255; fancyindex_time_format "%Y-%m-%d %H:%M:%S"; fancyindex_header "/one_theme/theme-resource-packages/header.html"; fancyindex_footer "/one_theme/theme-resource-packages/footer.html"; # fancyindex_ignore "/fancyindex_theme"; } location /one_theme { alias /etc/nginx/fancyindex_theme; try_files $uri $uri/ =404; }
方案要(贰)
索引目录与主题目录分开,主题目录不使用别名(alias)映射,直接引入相对于 location /(网站根)的相对路径(主题资源包引用路径亦同)
# 这里的/two是相对于网站根(/)的下一级目录 location /two { include /path/fancyindex.conf; }
# FileName:fancyindex.conf fancyindex on; fancyindex_localtime on; fancyindex_exact_size off; # 如下路径相对于网站根(/)而言的相对路径 fancyindex_header "/theme/theme-resource-packages/header.html"; fancyindex_footer "/theme/theme-resource-packages/footer.html"; fancyindex_ignore "theme"; fancyindex_name_length 255; fancyindex_time_format "%Y-%m-%d %H:%M:%S";
方案叁
1、如下,fancyindex.conf 必须是针对系统而言的绝对路径;
2、fancyindex.conf 里的 fancyindex_header 和 fancyindex_footer 路径要相对 location 的路径而言指定相对路径;
3、同样,到最终的主题资源里,也与第2点一样,指定针对 location 路径的相对路径。
location /eXhome { include /path/fancyindex.conf; }
# FileName:fancyindex.conf alias /path/example; fancyindex on; fancyindex_localtime on; fancyindex_exact_size off; fancyindex_header "/eXhome/theme/example/header.html"; fancyindex_footer "/eXhome/theme/example/footer.html"; fancyindex_ignore "fancyindex.conf"; # Ignored files will not show up in the directory listing, but will still be public. #fancyindex_ignore "theme"; # Making sure folder where files are don't show up in the listing. # Warning: if you use an old version of ngx-fancyindex, comment the last line if you # encounter a bug. See https://github.com/Naereen/Nginx-Fancyindex-Theme/issues/10 fancyindex_name_length 255; # Maximum file name length in bytes, change as you like.
- 注:若以上启用了 alias 别名,则后续的诸如 fancyindex_header、fancyindex_footer 及主题资源里的路径都要以别名(eXhome)为起点引入相应的路径。
Fancy Index Installing NGINX Fancy Index Module theme th2 th3 TheInsomniac green chinese char 内置主题语法 alilias 参考 研究研究 语法中文说明 密钥验证 go
- 扩展知识
nginx的location、root、alias指令用法和区别
nginx指定文件路径有两种方式root和alias,指令的使用方法和作用域:
[root]
语法:root path
默认值:root html
配置段:http、server、location、if
[alias]
语法:alias path
配置段:location
root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
root实例
location ^~ /t/ { root /www/root/html/; }
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。
alias实例:
location ^~ /t/ { alias /www/root/html/new_t/; }
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。
注意:
- location 后面跟路径处目录末尾假如有斜框,那么,alias的路径末尾也须有斜杠。
- alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
- alias只能位于location块中。(root可以不放在location中)