Choosing backend based on header value

Hi All,

I have a requirement wherein I need to choose a backend based on the
value of a header sent by the client. But in all other cases where the
header is not set I would like to use the available upstream block
with whatever logic (wr/r) it is set to.

For eg:

upstream backend_boxes {
server 192.168.1.30:9001 weight=25;
server 192.168.1.45:9001 weight=75;
}

locatinon ~ ^/test {
proxy_pass http://backend_boxes;
}

But, say If X-Caching-Node is set to 192.168.1.50:9001 , I want the
/test request to go to 192.168.1.50:9001.

I guess that should be possible?

Thanks
Harish

}

locatinon ~ ^/test {
proxy_pass http://backend_boxes;
}

But, say If X-Caching-Node is set to 192.168.1.50:9001 , I want the
/test request to go to 192.168.1.50:9001.

I guess that should be possible?

Sure. Be careful though, specify allowed nodes explicitly.

map $http_x_caching_node $x {
default backend_boxes;
192.168.1.50:9001 192.168.1.50:9001;
}

server {
location /test/ {
proxy_pass http://$x;
}
}

Hi,
Something that I could now quickly think of is. (thought not sure if
it’ll work)

map $http_x_caching_node $x {
default backend_boxes;
192.168.1.50:9001 192.168.1.50:9001;
}

Changing the map block to:

map $http_x_caching_node $x {
default backend_boxes;
~^(\d.*) $1;
}

Maybe there is a better way.

Thanks
Harish

This is exactly what I thought. But then is there a way without
explicitly specifying the nodes.?
I understand the pitfalls, its just to be used for testing purposes.
(like sending a request to a test server)

There is:

map $http_x_caching_node $x {
default backend_boxes;
~. $http_x_caching_node;
}

Hi Alexandr,
This is exactly what I thought. But then is there a way without
explicitly specifying the nodes.?
I understand the pitfalls, its just to be used for testing purposes.
(like sending a request to a test server)

Thanks
Harish

That looks good. Thanks a ton.

Harish.