nginx + upstreams and switch upstreams by arg from url

222 views Asked by At

I searched many forums , found many similar topics, but none works for me( I have this configuration:

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;
}

But after going by url site/?upstream=8084 I have no switching to 8084 upstream. If I test my config by changing to:

        if ($arg_upstream = "8083") {
                return 200 "upstream 8083"
        }
        if ($arg_upstream = "8084") {
                return 200 "upstream 8084"
        }

I see text perfectly like needed! Where am I going wrong? Thanks!

1

There are 1 answers

0
Boris G. On

this is answer to my question, it was decided by map '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";
}

and location part:

location / {
    if ($upstream_cookie) { set $upstream $upstream_cookie; }
    if ($arg_upstream) { add_header Set-Cookie upstream=$arg_upstream always; }

    proxy_pass http://$upstream;
}