nginx ngx_http_sub_module使用

ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串,比如你想把响应内容中的‘iuwai’全部替换成‘aaaaa‘,这个模块已经内置在nginx中,但是默认未安装,需要安装需要加上配置参数:–with-http_sub_module

因为公司对外提供的接口(xml)格式中需要将里面的二级域名替换下,从代码方面确实可以修改,但是更麻烦,有了这个module替换字符串就方便多了:

1.先查看是否有这个module:

./sbin/nginx -V

 

如果没有则需要重新编译安装:

./configure--prefix=/data01/nginx --with-pcre=/data01/pcre-8.35 --without-http_gzip_module --with-http_sub_module

make && make install

安装完毕需要修改nginx.conf

location / {
            root   html;
            index  index.html index.htm;
            sub_filter iuwai aaaaaa;
            sub_filter_types *;
            sub_filter_once off;
        }

官方示例:

location / {
    sub_filter      </head>
        '</head><script language="javascript" src="$script"></script>';
    sub_filter_once on;
}

 

然后重启./sbin/nginx -s reload

ok!

以上方法只能做一次替换,不能有多个字符串的不同替换,HttpSubsModule这个模块就可以提供多个替换方式:

首先需要安装这个模块,

模块下载地址:

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.替换页面中的敏感字

转载于:https://www.cnblogs.com/iuwai/p/4432084.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值