1. >>ngx_http_substitutions_filter_module OR HttpSubModule ?
为了应急处理或者一些需要,有时候需要使用Nginx的反向代理某站点,并通过 HttpSubModule 和ngx_http_substitutions_filter_module 模块替换正文内容和URL。
但是通常LNMP套件安装的webserver并没有编译安装nginx官方模块HttpSubModule(官方option),并且,官方自带的模块 HttpSubModule 只能匹配1条规则,但是使用第三方模块ngx_http_substitutions_filter_module 可以匹配多条规则。
备注:
ngx_http_substitutions_filter_module 是指第三方nginx模块 substitutions4nginx (原:Google Code 现:github)
HttpSubModule 是指Nginx官方的 with-http_sub_module模块(option)
Nginx自身带的module并不多,这也是它为什么性能好,系统开销较小的原因之一,相比apache,它不能动态的加载module,如果之前编译安装了Nginx,这时候就需要重新编译nginx添加模块,并替换掉原先的nginx执行文件。
2. 1.下载需要的文件
substitutions4nginx github下载
# 下载第三方模块
# cd ~
# git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
3. 2.查看之前Nginx编译configure
# nginx -V
nginx version: nginx/1.2.7
built by gcc 4.6.3(Ubuntu/Linaro4.6.3-1ubuntu5)
TLS SNI support enabled
configure arguments:--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with- http_ssl_module --with-http_gzip_static_module --with-ipv6
因为Nginx编译安装第三方模块的时候需要添加上之前编译的configure参数,然后重新设置configure编译(但是不覆盖安装,只make不install):
./configure --prefix=/你的安装目录--add-module=/第三方模块目录
4. 3.重新编译Nginx
# 打开Nginx编译目录,版本号可能不同
# cd ~/lnmp1.0-full/nginx-1.2.7
# 重新configure
# ./configure --prefix= --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=/root/ngx_http_substitutions_filter_module
# make
备注:重新编译的时候,记得一定要把以前编译过的模块一同加到configure参数里面.
5. 4.覆盖原nginx文件
# /etc/init.d/nginx stop
# cd cd objs/
# 覆盖原文件
# cp nginx /usr/local/nginx/sbin/
# /etc/init.d/nginx start
6. 5.简单配置
7. ① HttpSubModule 的 官方文档 说的很清楚,这里就不写实例了,并且功能没有substitutions4nginx的强大。
7.1. 在头部引入指定JS
location /{
sub_filter </head>
'</head><script language="javascript" src="$script"></script>';
sub_filter_types text/html;
sub_filter_once on;
}
- sub_filter 一行代码前面是需要替换的内容,后面单引号内是替换成的内容。
- sub_filter_once 意思是只查找并替换一次。on是开启此功能,off是关闭——默认值是on。
- sub_filter_types 一行意思是选定查找替换文件类型为文本型。也可以不加此行,因为默认只查找text/html文件。
- sub_filter模块可以用在http, server, location模块中。主要作用就是查找替换文件字符。
8. ② substitutions4nginx
8.1. subs_filter
实例:
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;
}
g(default):替换所有匹配的字符串。
i: 执行不区分大小写的匹配。
o: 只需将第一个。
r:该模式是作为一个正则表达式处理,默认是固定的字符串。
8.2. subs_filter_types
syntax: subs_filter_types mime-type [mime-types]
default: subs_filter_types text/html
context: http, server, location
subs_filter ‘<(no?script.*?)>(.*?)<(\/no?script.*?)>’” gi;//替换掉全部的<noscript></noscript>
subs_filter ‘<(s?cript.*?)>(?:\s|\S)*?<(\/s?cript.*?)>’” gi;//替换掉全部的<script>包换中间换行</script>
subs_filter ‘<(i?frame.*?)>(.*?)<(\/i?frame.*?)>’” gi;//替换<iframe></iframe>
9. >>参考资料<<
./configure --prefix=/alidata/server/nginx --add-module=../nginx/ngx_http_substitutions_filter_module
Installation
You need to install the sregex library first:
https://github.com/agentzh/sregex
And then rebuild your Nginx like this:
./configure --add-module=/path/to/replace-filter-nginx-module
If sregex is not installed to the default prefix (i.e., /usr/local
), then you should specify the locations of your sregex installation via the SREGEX_INC
and SREGEX_LIB
environments before running the ./configure
script, as in
export SREGEX_INC=/opt/sregex/include
export SREGEX_LIB=/opt/sregex/lib
assuming that your sregex is installed to the prefix /opt/sregex
.
https://github.com/openresty/replace-filter-nginx-module/blob/master/README.markdown
git clone https://github.com/openresty/replace-filter-nginx-module.git
./configure --prefix=/alidata/server/nginx --add-module=../nginx/replace-filter-nginx-module
make & make install
相关推荐
生产版nginx最新版本Dockerfile 添加主动检查nginx_upstream_check_module等第三方模块,
已经添加了http_image_filter_module模块的nginx版本,包含了libgd.dll,nginx版本1.15.9,windows64位版本
使用nginx作为http/https正向代理ipm包,包含ngx_http_proxy_connect_module 模块,附带了第三方图片代理配置,带有缓存,可直接做图片服务器 下载后执行:rpm -ivh nginx-1.12.2-1.el7_4.ngx.x86_64.rpm 打包教程:...
这里以安装第三方ngx_http_google_filter_module模块为例 nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so 1. 下载第三方扩展模块ngx_http_google_filter_module # cd /data/software/ # git...
nginx1.12.2源码、pcre-8.4.3源码、zlib-1.2.11源码包、openssl-1.0.2r源码包、echo-nginx-module-master模块源码、headers-more-nginx-module-master源码包、ngx_http_substitutions_filter_module源码包;...
备用nginx代理模块
nginx_tcp_proxy_module-master.zip
lua-upstream-nginx-module, Nginx C 模块将Lua向ngx_lua公开,用于 Nginx upstreams 电子邮件名称ngx_http_lua_upstream - Nginx MODULE,用于向 Nginx upstreams公开Lua到 ngx_lua目录NAME状态概要说明函数get_...
Nginx所需环境的安装,及nginx安装,nginx相关功能开启,后端节点健康检查插件安装,静态资源缓存插件,缩略图插件安装等。
添加nginx-http-flv-module模块并重新编译后的nginx(windows版)
二. ngx_http_limit_conn_module指令解释limit_conn_zone语法: limit_conn_zone $variable zo
下载后,在编译安装nginx时,用--add-module选项,指到sticky所在目录。类似命令如下: ./configure --prefix=/usr/local/nginx-1.6.0 --add-module=../nginx-sticky-module-1.25 --without-http_ssi_module --...
这里以安装第三方ngx_http_google_filter_module模块为例 nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so 1. 下载第三方扩展模块ngx_http_google_filter_module # cd /data/software/ # git ...
OpenResty 是一个通过扩展 nginx 的快速 Web 应用服务器。 Nginx Openresty For Windows (NOW) 是带有 Openresty 的 Windows 版本中的 Nginx。 它有一些特点: 高性能 并发两万多个连接 多进程 支持共享内存 支持...
官方nginx 镜像不带主动健康,本镜像将 nginx_upstream_check健康检查 打包到了镜像中。
nginx扩展工具,nginx的headers_more模块用于 添加、修改或清除 请求/响应头,该模块不是nginx自带的,默认不包含该模块,需要另外安装。幸运的是openresty默认包含了该模块,可以直接使用。 该模块主要有4个指令...
nginx_http_push_module 基于nginx的web push实现。
请在编译安装nginx-1.9.11前配置 ./configure --prefix=/opt/nginx-1.9.11 --add-module=/你的目录位置/fastdfs-nginx-module-master/src --with-http_ssl_module --with-stream 然后再make & make install ,最后在...
windows平台nginx编译nginx-http-flv-module,可直接运行,基于nginx-1.17.10编译
ngx_http_hls_module Nginx HLS 模块,兼容直播和点播服务.