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

Posted Fantasy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用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;
ClienIP=ngx.req.get_headers()["X-Real-IP"]
if ClientIP == nil then
    ClientIP = ngx.req.get_headers()["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;
    }
}

以上是关于利用nginx实现生产和灰度环境流量切换的主要内容,如果未能解决你的问题,请参考以下文章

通过生产环境流量录制/线下回放 配合基于云原生的灰度发布 实现保质保量的持续集成

通过生产环境流量录制/线下回放 配合基于云原生的灰度发布 实现保质保量的持续集成

通过生产环境流量录制/线下回放 配合基于云原生的灰度发布 实现保质保量的持续集成

通过生产环境流量录制/线下回放 配合基于云原生的灰度发布 实现保质保量的持续集成

Nginx流量复制

Nginx这个功能厉害了!