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

  • 相关阅读:
    图形与文本
    Cookie处理函数练习
    jspSmartUpload上传下载全攻略
    SmartUpload 上传图片
    无下拉菜单
    servlet中使用SmartUpload组件实现上传
    乱码
    DIV+CSS 要兼容 IE8.0 应注意些什么?
    虚拟目录中的web.config不被上级目录的web.config影响的处理
    ASP.NET抓取页面源代码
  • 原文地址:https://www.cnblogs.com/iuwai/p/4432084.html
Copyright © 2020-2023  润新知