nginx替换响应头(重点:如何在替换时加上if判断)

  在实现微信小程序内嵌非业务域名时,经过nginx作镜像网站绕太小程序业务域名检测,但有一些表单页面提交后会返回一个302状态,由响应头Location的值决定提交成功后的跳转地址。那么问题来了,这个地址也是属于非业务域名,这个时候咱们就须要将这个响应头也替换掉,那么nginx如何替换响应头呢,请看下面教程:php

  1、安装使用ngx_headers_more模块定制响应头:html

    ngx_headers_more 用于添加、设置和清除输入和输出的头信息。nginx没有内置该模块,须要另行添加。nginx

    (1)下载地址:https://github.com/openresty/headers-more-nginx-module/tagsgit

    (2)平滑升级nginx(参考上篇为nginx平滑添加SSL模块的文章:http://www.cnblogs.com/kenwar/p/8295907.html 添加参数add-module=/解压缩后文件路径)github

    (3)指令说明(我这边只用到设置响应头的指令,因此只介绍一个指令,若有兴趣请自行查找其使用说明):      正则表达式

        more_set_headers
        语法:more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...
        默认值:no
        配置段:http, server, location, location if
        阶段:输出报头过滤器
        替换(若有)或增长(若是不是全部)指定的输出头时响应状态代码与-s选项相匹配和响应的内容类型的-t选项指定的类型相匹配的。
        若是没有指定-s或-t,或有一个空表值,无需匹配。所以,对于下面的指定,任何状态码和任何内容类型都讲设置。小程序

        more_set_headers "Server: my_server";
        more_set_headers "Server: my_server";

        具备相同名称的响应头老是覆盖。若是要添加头,能够使用标准的add_header指令代替。
        单个指令能够设置/添加多个输出头。如:后端

        more_set_headers 'Foo: bar' 'Baz: bah';
        more_set_headers 'Foo: bar' 'Baz: bah';

        在单一指令中,选项能够屡次出现,如:微信小程序

        more_set_headers -s 404 -s '500 503' 'Foo: bar';

        more_set_headers -s 404 -s '500 503' 'Foo: bar';

        等同于:微信

        more_set_headers -s '404 500 503' 'Foo: bar';

        more_set_headers -s '404 500 503' 'Foo: bar';

        新的头是下面形式之一:
        Name: Value
        Name:
        Name
        最后两个有效清除的头名称的值。Nginx的变量容许是头值,如:

        set $my_var "dog";
        more_set_headers "Server: $my_var";
        set $my_var "dog";
        more_set_headers "Server: $my_var";

        注意:more_set_headers容许在location的if块中,但不容许在server的if块中。下面的配置就报语法错误:

        # This is NOT allowed!
        server {
        if ($args ~ 'download') {
          more_set_headers 'Foo: Bar';
        }
        ...
        }

  2、简单替换302状态下的响应头Location:

    location /{
      more_set_header "Location" "https://www.demo.com/xxx/index.html"        
    }

  3、(重点)使用正则表达式有选择的替换Location:

    咱们若是须要根据响应头里的内容来选择何种替换方式,该怎么作?

    需求:在location中判断响应头Location字段若是值为a(假设值),则将Location设置为b,其余忽略

    (1)初步尝试:

    location /{
      if($upstream_http_Location ~ a){
        more_set_header "Location" "https://www.demo.com/xxx/index.html"
      }
        
    }

      然而这里的if怎么都进不去,经过google得知是由于,在请求传递到后端以前,if就已经判断了,因此$upstream_http_Location是没有值的,这里能够使用一个map来有根据响应头的值有条件的执行操做

 map $upstream_http_Location $location{
    ~a https://www.democom/xxx/success.html;
    default $upstream_http_Location;
}
server{
    listen 80;
    listen 443 ssl;
    ssl_certificate /usr/local/nginx/ssl/www3.xiaolintong.net.cn/www3.xiaolintong.net.cn-ca-bundle.crt;
    ssl_certificate_key /usr/local/nginx/ssl/www3.xiaolintong.net.cn/www3.xiaolintong.net.cn.key;
    autoindex on;
    #开启读取非nginx标准的用户自定义header(开启header的下划线支持)
    underscores_in_headers on;
    server_name xxxxxxxxx;
    access_log /usr/local/nginx/logs/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
........
     location ^~/f/ {
     more_set_headers -s '302' 'Location $location';

 }
.......
}

      这里仅提供一个思路,请根据本身的需求灵活的使用map

相关文章
相关标签/搜索