# Choosing backend based on header value

**URL:** <https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612>\
**Category:** NGINX\
**Created:** [September 4, 2012, 12:56am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612 "2012-09-04T00:56:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 4, 2012, 12:56am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612/1 "2012-09-04T00:56:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 1:19am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612/2 "2012-09-04T01:19:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 1:45am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612/3 "2012-09-04T01:45:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 1:49am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612/4 "2012-09-04T01:49:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 1:29am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612/5 "2012-09-04T01:29:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harish\_sri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a88e57/32.png) [@harish\_sri](https://www.ruby-forum.com/u/harish_sri)\
**Post date:** [September 4, 2012, 2:27am UTC](https://www.ruby-forum.com/t/choosing-backend-based-on-header-value/222612/6 "2012-09-04T02:27:44Z")

</div>

That looks good. Thanks a ton.

Harish.
