利用nginx实现生产和灰度环境流量切换

1、根据cookie实现灰度配置

查询cookie键为version的值,如果该cookie值为V1则转发到server_01,为V2则转发到server_02,cookie值都不匹配的情况下默认走server_01所对应的服务器。

(1)、使用if指令实现

upstream server_01 {
    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
upstream server_02 {
    server 192.168.1.101:8080 max_fails=1 fail_timeout=60;
}
server {
    listen 80;
    server_name www.server.com;

    #match cookie
    set $server "server_01";
    if ($http_cookie ~* "version=V1"){
        set $server server_01;
    }
    if ($http_cookie ~* "version=V2"){
        set $server server_02;
    }
    location / {
        proxy_pass http://$server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.html index.htm;
    }
}

(2)、使用map指令实现

upstream server_01 {
    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
upstream server_02 {
    server 192.168.1.101:8080 max_fails=1 fail_timeout=60;
}
map $COOKIE_version $server {
    ~*V1$ server_01;
    ~*V2$ server_02;
    default server_01;
}
server {
    listen 80;
    server_name www.server.com;

    location / {
        proxy_pass http://$server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.html index.htm;
    }
}

2、根据权重实现灰度配置

upstream server {
    server 192.168.1.100:8080 max_fails=1 fail_timeout=60 weight=5;
    server 192.168.1.101:8080 max_fails=1 fail_timeout=60 weight=1;
}
server {
    listen 80;
    server_name www.server.com;

    location / {
        proxy_pass http://server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.html index.htm;
    }
}

3、根据来路IP实现灰度配置

(1)、资源在不同服务器上

upstream server_01 {
    server 192.168.1.100:8080 max_fails=1 fail_timeout=60;
}
upstream server_02 {
    server 192.168.1.101:8080 max_fails=1 fail_timeout=60;
}
server {
    listen 80;
    server_name www.server.com;

    set $server "server_01";
    if ($remote_addr ~ "211.118.119.11"){
        set $server server_02;
    }
    location / {
        proxy_pass http://$server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.html index.htm;
    }
}

(2)、资源在同一服务器上

server {
    listen 80;
    server_name www.server.com;

    set $rootdir "/var/www/html";
    if ($remote_addr ~ "211.118.119.11"){
        set $rootdir "/var/www/test";
    }
    location / {
        root $rootdir;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.html index.htm;
    }
}

4、Nginx+Lua实现灰度配置

#如果来源IP地址是ipList.conf中的地址,那么就定位到test_env,
#如果来源IP地址不在ipList.conf中(即有效用户IP),那么就定位到product_env。

local ip_config = ngx.shared.config;
local ClienIP = ngx.var.http_x_real_ip
if ClientIP == nil then
    ClienIP = ngx.var.http_x_forworded_for
end
if ClientIP == nil then
    ClientIP = ngx.var.remote_addr
end
for line in io.lines("/usr/local/nginx/conf/lua/ipList.conf") do
    if not ip_config:get(line) then
        ip_config:set(line, "0")
    end
end
if ip_config:get(ClientIP) == "0" then
    ngx.exec("@test_env")
else
    ngx.exec("@product_env")
end

ipList.conf内容格式
XXX.XXX.XXX.XXX
YYY.YYY.YYY.YYY

lua_code_cache on;
lua_shared_dict config 1m;

upstream MyServer {
    server 192.168.1.199:8099 max_fails=3 fail_timeout=30s;
    server 192.168.1.200:8099 max_fails=3 fail_timeout=30s;
    ip_hash;
}
server {
    listen 80;
    server_name jokerzhang.cn;
    access_log off;
    #access_log logs/jokerzhang.log;

    location / {
        access_by_lua_file /usr/local/nginx/conf/lua/ip_gray.lua;
    }
    location @test_env {
        proxy_pass http://192.168.1.199:8099;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location @product_env {
        proxy_pass http://MyServer;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值