nginx替换响应内容

  因为想要将非业务域名内嵌到微信小程序中,所以用到了nginx的反向代理功能来替换域名实现盗站(缘起:http://www.cnblogs.com/kenwar/p/8288882.html),但是替换域名后问题来了,因为XmlHttpRequest同源策略:“禁止向不同源的地址发起HTTP请求” 所以光替换域名还是不够,还需要替换响应内容里的一些ajax请求,这就涉及到了nginx的响应内容替换功能。

  一、简单替换模块 ngx_http_sub_module:

    (1)介绍:

      ngx_http_sub_module模块是一个过滤器,它修改网站响应内容中的字符串。这个模块已经内置在nginx中,但是默认未安装,需要安装需要加上配置参数:--with-http_sub_module 如果已经安装nginx,只需要再添加这个模块就可以了。

    (2)常用指令:

      2.1 sub_filter指令: sub_filter string(原字符串) replacement(用于替换的字符串);

        用于设置需要使用说明字符串替换说明字符串.string是要被替换的字符串,replacement是 新的字符串,它里面可以带变量。

      2.2 sub_filter_last_modified指令: sub_filter_last_modified on | off;

        用于设置网页内替换后是否修改 可在nginx.conf的 http, server, location三个位置配置使 用,默认值是off;

      2.3 sub_filter_once指令:sub_filter_once on | off;

        用于设置字符串替换次数,默认只替换一次。如果是on,默认只替换第一次匹配到的到字 符,如果是off,那么所有匹配到的字符都会被替换;

      2.4 sub_filter_types指令:sub_filter_types * 

        用于指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有的;

    (3)说明:

      3.1以上指令可在nginx.conf的http, server, location三个位置配置使用;

      3.2此模块替换不区分大小写;

      3.3支持中文替换;

    (4)示例:     

location ^~/im/ {
    proxy_pass http://p.qiao.baidu.com;
    add_header Access-Control-Allow-Origin *;
    sub_filter 'http://p.qiao.baidu.com' 'https://www.demo.com';##将响应内容中的域名替换成本站域名
    sub_filter_once off;####所有匹配到的都替换
}

    (5)总结:

       使用ngx_http_sub_module模块好处是nginx内置该模块使用方便,不足之处在于该模块不支持正则替换,灵活性不够

  二、支持正则匹配替换的第三方模块ngx_http_substitutions_filter_module:

    (1)下载地址:https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip

    (2)解压缩,平滑升级nginx(升级方式可参考前一篇文章平滑添加ssl模块:http://www.cnblogs.com/kenwar/p/8295907.html (添加参数--add-module=/模块解压后文件路径))

    (3)使用说明:

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;
    subs_filter http://$host https://$host;
}
从github给出的使用示例来看,这个模块涉及2个指令:
* subs_filter_types
subs_filter_types 语法: subs_filter_types mime-type [mime-types]
默认: subs_filter_types text/html
适用: http, server, location
subs_filter_types 是用来指定替换文件类型的 默认仅仅替换text/html类型的文件。

* subs_filter
subs_filter 语法: subs_filter source_str destination_str [gior]
默认: none
适用: http,server,location
subs_filter 是用来替换文本的,可以使用正则
g(默认):替换匹配项。
i  :区分大小写的匹配
o : 只匹配发现的第一个。
r  : 正则匹配。

    (4)案例分享:

        替换响应内容中的某个js,因为js版本不固定,路径是变动的,所以用到正则

location ^~/f/ {
         proxy_pass http://xxx.xxxx.com;
         add_header Access-Control-Allow-Origin *;
     subs_filter 'https://cdn.jinshuju.net/assets/published_forms/mobile/application-(.*?(.js){1})' 'https://www.demo.com/comm/js/jinshujujsforsmallproject.js' ir ;
 }

    (5)说明:

       在使用正则模式时,不能使用nginx内置变量,比如:$host,否则会出现如下报错:
        nginx: [emerg] match part cannot contain variable during regex mode in *

原文地址:https://www.cnblogs.com/kenwar/p/8296508.html