Score:0

nginx proxy_next_upstream with upstream variable

us flag

I find it very convenient (mainly for testing) to be able to change the upstream according to an url parameter:

upstream balance {
  server 111.111.111.111 weigth=5;
  server 222.222.222.222 weigth=6;
}

upstream server1 {
  server 111.111.111.111;
}

upstream server2 {
  server 222.222.222.222;
}

server {
  server_name myserver.servers.com;

  set $upstream balance;

  if ($arg_server = "server1") {
        set $upstream server1;
    }

    if ($arg_server = "server2") {
        set $upstream server2;
    }
  
  location / {
        proxy_pass http://$upstream/VirtualHostBase/https/$host:80/VirtualHostRoot$request_uri;
        proxy_next_upstream http_500 http_502;
    }
  listen: 80;
}

The upstream switch works, but in this configuration, the directive proxy_next_upstream does not work, because of the $upstream variable in proxy_pass (because nginx can only determine the upstream server on request and proxy_next_upstream is determined on server start or reload if my understanding is right).

Is there a way to use this url parameter and still get proxy_next_upstream working? I understand that I could use subdomains instead of url parameters, but that's something I'd like to avoid.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.