nginx + 上游并通过 arg 从 url 切换上游

问题描述

搜索了很多论坛,找到了很多类似的主题,但没有一个适合我( 我有这个配置:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...