前言

该文章旨在了解相关方案,并非最佳解法~
由于项目的灰度偏业务,发布平台的灰度方案并不能支持,于是便考虑在网关层(类似 Nginx)上考虑使用 cookie 判断来分流灰度用户。
出 Nginx 方案是方便本地调试~

Nginx.conf

$COOKIE_gray gray 为 cookie key
true,false 为命中逻辑,可为正则

http {
    include       mime.types;
    default_type  application/octet-stream;


    map $COOKIE_gray $group {
        true apache001;
        false apache002;
        default apache002;
    }

    upstream apache001 {
        server 192.168.1.101:3001 weight=1 max_fails=1 fail_timeout=30s;
    }

    upstream apache002 {
        server 192.168.1.102:8000 weight=1 max_fails=1 fail_timeout=30s;
    }


    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       3000;
        server_name  localhost;

        location / {
            proxy_pass http://$group;
        }
    }
    include servers/*;
}