# Load balancing based on hash of the domain

**URL:** <https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642>\
**Category:** NGINX\
**Created:** [December 15, 2015, 10:13am UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642 "2015-12-15T10:13:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [December 15, 2015, 10:13am UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642/1 "2015-12-15T10:13:54Z")

</div>

Hi all,

I wish to use load balancing in front of a shared webhosting cluster  
with multiple domains.  
To make optimal use of resources, it would make sense to do load  
balancing based on domain instead of random or based on IP.  
I noticed that there is a hash parameter but I’m unable to use  
$server\_name or $host as a parameter, only $request\_uri and  
$remote\_addr.

Is there any way that I could use $server\_name?

In my opinion it makes no sense to randomize all requests for the  
different domains and would make sense to load balance based on a hash  
of the domain.

Regards,

Bram

---

<div class="post-metadata">

**Author:** ![Bram\_Verdonck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/35a633/32.png) [@Bram\_Verdonck](https://www.ruby-forum.com/u/Bram_Verdonck)\
**Post date:** [December 15, 2015, 10:58am UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642/2 "2015-12-15T10:58:53Z")

</div>

On Tuesday 15 December 2015 10:13:35 Bram Verdonck wrote:

> 

What’s the problem with the “hash” directive of upstream block?  
Why can’t you use it?

> **[Module ngx\_http\_upstream\_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#hash)**
>
> The ngx\_http\_upstream\_module module is used to define groups of servers that can be referenced by the proxy\_pass, fastcgi\_pass, uwsgi\_pass, scgi\_pass, memcached\_pass, and grpc\_pass directives.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Bram\_Verdonck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/35a633/32.png) [@Bram\_Verdonck](https://www.ruby-forum.com/u/Bram_Verdonck)\
**Post date:** [December 15, 2015, 11:57am UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642/3 "2015-12-15T11:57:18Z")

</div>

Problem is that it will not accept anything besides $remote\_addr and  
$request\_uri.  
For example:

```
upstream loadbalancer {
    hash $server_name consistent:
    #hash $request_uri consistent;
    server 10.0.0.1:8080;
    server 10.0.0.2:8080;
}

```

Will fail with:

invalid number of arguments in “hash” directive in  
/etc/nginx/nginx-loadbalancing.conf:6

---

<div class="post-metadata">

**Author:** ![Bram\_Verdonck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/35a633/32.png) [@Bram\_Verdonck](https://www.ruby-forum.com/u/Bram_Verdonck)\
**Post date:** [December 15, 2015, 12:24pm UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642/4 "2015-12-15T12:24:11Z")

</div>

On Tue, Dec 15, 2015 at 11:56:43AM +0100, Bram wrote:

Hi there,

> Problem is that it will not accept anything besides $remote\_addr and  
> $request\_uri.  
> For example:
> 
> ```
> upstream loadbalancer {
> hash $server_name consistent:
> 
> ```

Use ; not :

> ```
> #hash $request_uri consistent;
> server 10.0.0.1:8080;
> 
> ```

Line 6 is probably that one, because that is where the “hash” directive  
terminates.

> ```
> server 10.0.0.2:8080;
> }
> 
> ```
> 
> Will fail with:
> 
> invalid number of arguments in “hash” directive in  
> /etc/nginx/nginx-loadbalancing.conf:6

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Bram\_Verdonck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/35a633/32.png) [@Bram\_Verdonck](https://www.ruby-forum.com/u/Bram_Verdonck)\
**Post date:** [December 15, 2015, 12:27pm UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642/5 "2015-12-15T12:27:06Z")

</div>

On Tuesday 15 December 2015 11:56:43 Bram wrote:

> Problem is that it will not accept anything besides $remote\_addr and  
> $request\_uri.  
> For example:
> 
> ```
> upstream loadbalancer {
> hash $server_name consistent:
> 
> ```

There’s a typo: you’ve used a colon at the end of the directive.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Bram\_Verdonck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/35a633/32.png) [@Bram\_Verdonck](https://www.ruby-forum.com/u/Bram_Verdonck)\
**Post date:** [December 15, 2015, 12:32pm UTC](https://www.ruby-forum.com/t/load-balancing-based-on-hash-of-the-domain/243642/6 "2015-12-15T12:32:22Z")

</div>

Wow, was struggling with that for a week already.  
Can’t believe that was it.

Thank you Valentin and Francis!
