灰度发布(也称为金丝雀发布)是一种逐步将新版本软件发布给部分用户的技术。以下是几种使用Nginx实现灰度发布的常见方法:
upstream production {
server 192.168.1.100:8080;
}
upstream canary {
server 192.168.1.101:8080;
}
server {
listen 80;
set $upstream "production";
if ($http_cookie ~* "canary=true") {
set $upstream "canary";
}
location / {
proxy_pass http://$upstream;
}
}
map $http_x_canary $upstream {
default "production";
"true" "canary";
}
upstream production {
server 192.168.1.100:8080;
}
upstream canary {
server 192.168.1.101:8080;
}
server {
listen 80;
location / {
proxy_pass http://$upstream;
}
}
geo $is_canary {
default 0;
192.168.1.50/32 1; # 特定IP段
10.0.0.0/8 1; # 内网用户
}
upstream production {
server 192.168.1.100:8080;
}
upstream canary {
server 192.168.1.101:8080;
}
server {
listen 80;
location / {
if ($is_canary) {
proxy_pass http://canary;
}
proxy_pass http://production;
}
}
upstream backend {
server 192.168.1.100:8080 weight=90; # 90%流量到生产
server 192.168.1.101:8080 weight=10; # 10%流量到灰度
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
map $arg_userid $upstream {
default "production";
"~*^123" "canary"; # 用户ID以123开头的走灰度
}
upstream production {
server 192.168.1.100:8080;
}
upstream canary {
server 192.168.1.101:8080;
}
server {
listen 80;
location / {
proxy_pass http://$upstream;
}
}
Nginx Plus提供更高级的灰度发布功能: - 主动健康检查 - 更精细的流量分割 - 会话持久性 - 高级监控
以上方法可以根据实际需求组合使用,实现更灵活的灰度发布策略。