通过cookie实现外网访问到指定服务器

一、前端调用接口注入cookie值runEnv

1.1 调用设置灰度接口,让本机中的cookie的runEnv设置为 "hd"

https://hx168.moguyun.com/nx/sethd

1.2 调用设置访问环境为生产的接口,让本机中的cookie的runEnv设置为"delete"

https://hx168.moguyun.com/nx/setsc

1.3 调用查询当前环境接口,返回cookie中的runEnv字段值

https://hx168.moguyun.com/nx/read

1.4 前面接口实现过程

location /nx/sethd {
    add_header "Set-Cookie" "runEnv=hd;domain=$host;path=/;expires=Mon, 03 Sep 2099 05:03:39 GMT";
    default_type text/html;
    return 200 'huidu';
}

location /nx/setsc {
    default_type text/html;
    add_header "Set-Cookie" "runEnv=delete;domain=$host;path=/;expires=Mon, 03 Sep 2011 05:03:39 GMT";
    return 200 'prod';
}

location /nx/read {
    default_type text/html;
    return 200 "env == $COOKIE_runEnv";
}

二、配置灰度服务器地址upstream配置

2.1 配置financial-shop的灰度服务器ip:port

upstream financialServer-b-hd {
        server 10.50.X.221:8207;
}

三、判断传入生产还是灰度服务器地址

3.1 根据runEnv的值判断是转发到灰度还是生产

location /financial-shop-server {
proxy_pass http://financialServer-b;
      if ( $COOKIE_runEnv = "hd" ) {
                proxy_pass http://financialServer-b-hd;
      }
...
}

四、引入common-b-hd.conf实现公网访问灰度环境

include common/common-b-hd.conf;
 
发布了161 篇原创文章 · 获赞 40 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_36441027/article/details/102687281