I have a scenario where I want to proxy_pass
requests to one of several upstream destinations based on a value which can appear in a header or as a query parameter.
Right now, I've got the header-based mapping mostly figured out:
map $http_x_destination $destination {
default upstream0;
something upstream1;
something2 upstream1;
something3 upstream2;
}
...
server {
location / {
proxy_pass https://$destination;
}
}
Obviously this works well for the cases where we have a header present, but not for those cases where the target is based on an operation. My initial instinct would be to use some sort of conditional logic to see if the header is present, and construct the map based on $http_x_target
if it is, or $arg_target
instead.
But I've read plenty of exhortations not to use if
, as it's considered dangerous, and since I don't know how nginx handles the scoping of definitions in the first place (let alone in if
blocks), I'm leery of this approach.
Is there a good way to merge these two sources of information in order to cleanly map their values to upstreams?