问题描述
我搜索了很多论坛,找到了很多类似的主题,但没有一个适合我( 我有这个配置:
upstream 8083 { server 127.0.0.1:8083; }
upstream 8084 { server 127.0.0.1:8084; }
split_clients "upstream${remote_addr}" $default {
50% 8083;
50% 8084;
}
map $arg_upstream $upstream {
default $default;
"8083" "8083";
"8084" "8084";
}
location / {
if ($arg_upstream = "8083") {
proxy_pass http://8083;
break;
}
if ($arg_upstream = "8084") {
proxy_pass http://8084;
break;
}
proxy_pass http://$default;
}
但是在通过 url site/?upstream=8084 之后,我没有切换到 8084 上游。 如果我通过更改为来测试我的配置:
if ($arg_upstream = "8083") {
return 200 "upstream 8083"
}
if ($arg_upstream = "8084") {
return 200 "upstream 8084"
}
我看到的文字完全符合需要!我哪里错了? 谢谢!
解决方法
这是我的问题的答案,它是由地图“http_cookies”决定的:
upstream default {
ip_hash;
server 127.0.0.1:8083;
server 127.0.0.1:8084;
}
upstream 8083 { server 127.0.0.1:8083; }
upstream 8084 { server 127.0.0.1:8084; }
map $arg_upstream $upstream {
'8083' '8083';
'8084' '8084';
default 'default';
}
map $http_cookie $upstream_cookie {
default '';
"~*upstream=(?<variable>[^;]+)" "$1";
}
和位置部分:
location / {
if ($upstream_cookie) { set $upstream $upstream_cookie; }
if ($arg_upstream) { add_header Set-Cookie upstream=$arg_upstream always; }
proxy_pass http://$upstream;
}