I have a set of upstream servers and I route requests to them by mapping their URI to an id which is then hashed in the upstream directive:
# URI mapping
map $uri $server_id {
"~someregex" $server;
default 1;
}
# Upstream
upstream servers {
hash $server_id;
server server-1;
server server-2;
# etc.
}
Now I need some requests to always be mapped to specific servers and some to be distributed (roughly) evenly across the servers. So my questions are:
- Can NGINX produce (without adding additional modules) a random variable so in case the URI can't be mapped, then the default is a random value?
- If random variables can't be produced, is there another approach that can conditionally choose load-balancing strategies based on whether the mapping was successful? (e.g. if the uri was mapped successfully use
hashin the upstream but if not userandom)
P.S. - I know that I could generate a random value on the client end and use that in the URI to tweak the routing (that's what I'm currently doing) but it leads to some hacky client side code and strange urls)