ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串,好比你想把响应内容中的‘iuwai’所有替换成‘aaaaa‘,这个模块已经内置在nginx中,可是默认未安装,须要安装须要加上配置参数:–with-http_sub_modulejavascript
由于公司对外提供的接口(xml)格式中须要将里面的二级域名替换下,从代码方面确实能够修改,可是更麻烦,有了这个module替换字符串就方便多了:css
1.先查看是否有这个module:html
./sbin/nginx -V
若是没有则须要从新编译安装:java
./configure--prefix=/data01/nginx --with-pcre=/data01/pcre-8.35 --without-http_gzip_module --with-http_sub_module make && make install
安装完毕须要修改nginx.confnginx
location / { root html; index index.html index.htm; sub_filter iuwai aaaaaa; sub_filter_types *; sub_filter_once off; }
官方示例:git
location / { sub_filter </head> '</head><script language="javascript" src="$script"></script>'; sub_filter_once on; }
而后重启./sbin/nginx -s reloadgithub
ok!网站
以上方法只能作一次替换,不能有多个字符串的不一样替换,HttpSubsModule这个模块就能够提供多个替换方式:spa
首先须要安装这个模块,code
模块下载地址:
git clone git://github.com/yaoweibin/ngx_http_substitutions_filter_module.git
从新编译:
./configure --prefix=/data01/nginx --with-pcre=/data01/pcre-8.35 --without-http_gzip_module --with-http_sub_module --add-module=/data01/ngx_http_substitutions_filter_module/ make && make install
官方给的实例:
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; }
个人配置:
location / { root html; index index.html index.htm; subs_filter iuwai aaaaaa; subs_filter baidu bbbbb; subs_filter_types *; #sub_filter_once off; }
OK,大功告成,返回的页面内容随意修改,很神奇吧!
结束语:
另外还能够指定只匹配一个、是否区分大小写、正则匹配替换。
此方式有如下功能:
1.如官方示例,须要添加js文件、或css样式文件
2.替换页面中的敏感字