8

Nginx内容替换模块http_substitutions_filter_module及实用案例分享

 3 years ago
source link: https://zhang.ge/5116.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Jager · 12月18日 · 2016年nginx · nginx配置 · wordpress 686次已读

说到Nginx的内容替换功能,大部分人应该都听说过Nginx内置的的subs_filter替换模块,但是这个模块有个缺憾,就是只能替换一次,而且还不支持正则表达式,这就有些鸡肋了。

不过,我们可以集成一个第三方的替换模块:ngx_http_substitutions_filter_module,来实现我们的各种需求。

经过测试,这个模块至少有如下实用功能:

①、支持多次替换

②、支持正则替换

③、支持中文替换

Ps:略有遗憾的是,这个替换不能使用到 if 判断模块内,否则就超神了。。。

下面,简单介绍下 ngx_http_substitutions_filter_module 的安装实用以及一些实用案例。

一、编译集成

和所有Nginx非内置模块一样,添加模块需要在编译的时候指定模块源码包来集成。当然,Tengine可以使用动态模块加载的功能,这里就不细说了。

①、下载模块源码包并解压,最后列出目录位置备用

[root@MyServer ~]# wget -O ngx_http_substitutions_filter_module-master.zip https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip
[root@MyServer ~]# unzip ngx_http_substitutions_filter_module-master.zip
[root@MyServer ~]# cd ngx_http_substitutions_filter_module-master && pwd
/root/ngx_http_substitutions_filter_module-master

github手动下载地址:https://github.com/yaoweibin/ngx_http_substitutions_filter_module/

②、在服务器上执行 nginx -V 查看当前  Nginx 编译参数,比如:

[root@MyServer ~]# /usr/local/nginx/sbin/nginx -V
Tengine version: Tengine/2.1.2 (nginx/1.6.2)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --add-module=../ngx_cache_purge-2.3 --with-http_sub_module

③、加上模块参数,重新编译Nginx

找到服务器上原来安装时留下的 Nginx 源码目录(如果没有请重新下载并解压,此处不赘述),进入目录后,在第②步中的参数基础上新增集成替换模块(请注意前面需要加上  ./configure ):

./configure [+原有参数+] --add-module=/root/ngx_http_substitutions_filter_module-master/

./configure --prefix=/usr/local/nginx --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --add-module=../ngx_cache_purge-2.3 --with-http_sub_module --add-module=/root/ngx_http_substitutions_filter_module-master/

再往后,则是make以及平滑升级,请参考之前的文章完成:

Nginx在线服务状态下平滑升级或新增模块的详细操作记录

正确完成后,Nginx就具备内容替换功能了。

二、使用说明

模块的github主页其实已经有了很详细的说明了,这里就简单的做下搬运工。

使用示例:

location / {
subs_filter_types text/html text/css text/xml;
subs_filter st(\d*).example.com $1.example.com ir;
subs_filter a.example.com s.example.com;
subs_filter http://$host https://$host;

从github给出的使用示例来看,这个模块涉及2个指令:

* subs_filter_types

subs_filter_types 语法: subs_filter_types mime-type [mime-types]

默认: subs_filter_types text/html

适用: http, server, location

subs_filter_types 是用来指定替换文件类型的 默认仅仅替换text/html类型的文件。

* subs_filter

subs_filter 语法: subs_filter source_str destination_str [gior]

默认: none

适用: http,server,location

subs_filter 是用来替换文本的,可以使用正则

g(默认):替换匹配项。

i  :区分大小写的匹配

o : 只匹配发现的第一个。

r  : 正则匹配。

三、案例分享

①、全站https

有了这个功能,要实现全站https也就是非常简单了,只要把本站的http://协议代码全部替换成https即可。当然,替换时要注意匹配范围,免得把不支持https的外链也一起替换了。。。

比如,将如下代码添加到网站Nginx配置内即可完成替换

location / { # 新增如下代码
subs_filter http:// https:// gr; #将页面中的http链接全部替换为https

②、CDN域名替换

这个模块在CDN方面同样简单实用!比如,我们网站要用到七牛CDN,不管是纯代码还是插件,那都是靠PHP代码来进行替换的,性能肯定就不如Nginx直接替换来的简单粗暴了。

location / { # 新增如下代码,将静态文件域名替换成七牛
subs_filter https://zhang.ge/([^"\']*?)\.(jpg|png|js|css|jpeg|bmp) https://example.qiniudn.com/$1.$2 igr;

Ps:经测试,在使用正则模式时,不能使用nginx内置变量,比如:$host,否则会出现如下报错:

nginx: [emerg] match part cannot contain variable during regex mode in ***

③、解决前台暴露管理员账号风险

前段时间,看到有博客在说 WordPress 会在前台暴露管理员登陆账户的问题,然后给出了较为复杂的解决办法:通过修改 WordPress 内核函数来隐藏账户名。

修改内核函数,一般是非常无奈,没有其他解决方法的时候才会用到,所以,我看到这个问题第一件时间想到的办法就是替换。

使用PHP替换是非常简单的,参考博客之前分享的文章即可搞定:

WordPress简单代码开启七牛CDN及集成七牛缩略图的方法

而对于本文来说,PHP替换方案依然过于复杂,因为本文一行规则即可解决问题,比如将登录名admin替换为null:

location / {
# 替换管理员登录名 admin 为 null
subs_filter 'author-admin' 'author-null';

其他登录名,请自行参考,若有多个登录名可以另起多行或使用正则表达式均可。

鉴于篇幅有限,就分享这三个实用技巧吧,其他就靠自己发掘了,比如镜像网站做内容替换什么的....


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK