# Load balancing according to url

**URL:** <https://www.ruby-forum.com/t/load-balancing-according-to-url/228581>\
**Category:** NGINX\
**Created:** [April 29, 2013, 8:28am UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581 "2013-04-29T08:28:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eswar\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e47774/32.png) [@eswar\_r](https://www.ruby-forum.com/u/eswar_r)\
**Post date:** [April 29, 2013, 8:28am UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581/1 "2013-04-29T08:28:11Z")

</div>

Hi All,

Can anyone please help me with the below requirement.

Host machine contains a plugin and it communicates with a plugin handler  
running on backend servers and nginx is used to load balance the  
requests.

host machine(plugin) ===== \>nginx as load balancer =====\>3 backend  
servers  
which hosts plugin handler

I need to load balance the requests based on customer-id field in host  
machine.

# curl ’

[http://localhost:8031/test1/test2/test3/customer/123456789999999999/......./](http://localhost:8031/test1/test2/test3/customer/123456789999999999/......./)

customer-id: 123456789999999999  
customer-id changes with customers.

Since the requests come from same machine, I can’t use ip\_hash or cookie  
based load balancing technique.

My requirement is to load balance according to customer id and if same  
request comes the same customer it should go to same earlier server  
which  
served the request.

I am planning to extract the customer-id in nginx configuration file and  
add them on the fly in the config file and compare the ids using “map”  
directive. But unable to know which server served the requests to  
customer-id:

map $customer\_id $sticky\_backend {  
default bad\_gateway;  
\<cust-id\_1\> \<server 1\>;  
\<cust-id\_2\> \<server 2\>;  
}  
if ( $request\_uri ~ ^/(._)/(customer)/(._?)/(.\*)/ ) {  
set $customer\_id $3;

Thanks  
Eswar

---

<div class="post-metadata">

**Author:** ![eswar\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e47774/32.png) [@eswar\_r](https://www.ruby-forum.com/u/eswar_r)\
**Post date:** [April 29, 2013, 9:27am UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581/2 "2013-04-29T09:27:13Z")

</div>

there’s a sticky-module (3rd-party), maybe this works out of the box  
for you.

[https://code.google.com/p/nginx-sticky-module/wiki/Documentation](https://code.google.com/p/nginx-sticky-module/wiki/Documentation)

Posted at Nginx Forum:

> **[Re: load balancing according to url](https://forum.nginx.org/read.php?2,238694,238700#msg-238700)**
>
> Hi All, Can anyone please help me with the below requirement. Host machine contains a plugin and it communicates with a plugin handler running on backend servers and nginx is used to load balance the requests. host machine(plugin) ===== \>nginx as...

---

<div class="post-metadata">

**Author:** ![eswar\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e47774/32.png) [@eswar\_r](https://www.ruby-forum.com/u/eswar_r)\
**Post date:** [April 29, 2013, 11:02am UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581/3 "2013-04-29T11:02:28Z")

</div>

You chain two map directives. Like this:

map $uri $customer\_id {  
~/customer/(?\<cust\_id\>[^/]+)/.\*$ $cust\_id;

}

map $customer\_id $sticky\_backend {  
default bad\_gateway;  
\<cust-id\_1\> \<server 1\>;  
\<cust-id\_2\> \<server 2\>;  
}

----appa

---

<div class="post-metadata">

**Author:** ![eswar\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e47774/32.png) [@eswar\_r](https://www.ruby-forum.com/u/eswar_r)\
**Post date:** [April 29, 2013, 1:05pm UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581/4 "2013-04-29T13:05:09Z")

</div>

Hi Antonio,

Thanks for the response.

I am unable to understand your solution.

As I said, I am unable to know which server served the requests to  
respective customer-id.  
So I cant write the below map directive.  
map $customer\_id $sticky\_backend {  
default bad\_gateway;  
\<cust-id\_1\> \<server 1\>;  
\<cust-id\_2\> \<server 2\>;  
}

Thanks  
Eswar

---

<div class="post-metadata">

**Author:** ![eswar\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e47774/32.png) [@eswar\_r](https://www.ruby-forum.com/u/eswar_r)\
**Post date:** [April 29, 2013, 1:09pm UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581/5 "2013-04-29T13:09:14Z")

</div>

Hi Mex,

Thanks for the response.

But I don’t think the sticky-module is going to help in my case where  
the  
load balancing is to be done based on a particular filed in the URL.

Thanks  
Eswar

---

<div class="post-metadata">

**Author:** ![eswar\_r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e47774/32.png) [@eswar\_r](https://www.ruby-forum.com/u/eswar_r)\
**Post date:** [April 29, 2013, 1:28pm UTC](https://www.ruby-forum.com/t/load-balancing-according-to-url/228581/6 "2013-04-29T13:28:31Z")

</div>

Hi.

Emm, maybe you want to make some kind of sharding by the value of your  
cookie?

upstream [http://wiki.nginx.org/NginxHttpUpstreamModule#upstream](http://wiki.nginx.org/NginxHttpUpstreamModule#upstream)  
backend {  
server [http://wiki.nginx.org/NginxHttpCoreModule#server](http://wiki.nginx.org/NginxHttpCoreModule#server) server1;  
server [http://wiki.nginx.org/NginxHttpCoreModule#server](http://wiki.nginx.org/NginxHttpCoreModule#server) server2;  
hash $customer\_id;}
