zoukankan      html  css  js  c++  java
  • 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.替换页面中的敏感字

  • 相关阅读:
    阅读计划
    第一课 课堂练习总结
    人月神话读后感
    软件工程概论11-软件演化
    【HDU4366】【DFS序+分块】Successor
    【转载】【元胞自动机】生命游戏(时间简史)
    【BZOJ2741】【块状链表+可持久化trie】FOTILE模拟赛L
    【BZOJ3295】【块状链表+树状数组】动态逆序对
    【HDU4391】【块状链表】Paint The Wall
    【POJ2887】【块状链表】Big String
  • 原文地址:https://www.cnblogs.com/iuwai/p/4432084.html
Copyright © 2011-2022 走看看