11

I'm currently running two back end servers on my network and load balancing with Nginx on Windows.

I am load testing the system at the moment however all of my traffic is directed at one server. This is because the ip_hash algorithm sorts traffics by the first 3 octets i.e. 111.222.333.XXX

This is a problem because all of the traffic I am aiming at the server has the same base address (The same first 3 octets) therefore none of my traffic is going to the other server. Does anyone know a way to patch or change the ip_hash algorithm to filter through 4 octets.

Thanks

Sprout
  • 630
  • 1
  • 5
  • 22
  • why not to try sticky session or other LB methods: round-robin, least-connected, hash? – Anatoly Sep 10 '15 at 18:44
  • I am using ip_hash. I have tried using round-robin and least connected however they casue errors and do not appear to be as quick in my situation. I didn't know you could sticky session in Nginx – Sprout Sep 11 '15 at 08:40
  • @Anatoly sorry I didn't realise there was something such as 'hash' – Sprout Sep 11 '15 at 09:32

1 Answers1

11

Nginx open source version supports the hash directive that may work similarly (not exactly the same though) to the sticky session mechanism provided by commercial version:

The generic hash method: the server to which a request is sent is determined from a user-defined key which may be a text, variable, or their combination. For example, the key may be a source IP and port, or URI:

upstream backend {
    hash $request_uri consistent;

    server backend1.example.com;
    server backend2.example.com;
}

https://www.nginx.com/resources/admin-guide/load-balancer/

So how do you use 4 octets from IPv4 with the hash method? Let's find how to get the client IP from the Embedded Variables section http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

$remote_addr client address

So the code looks like:

upstream backend {
    hash $remote_addr consistent;

    server backend1.example.com;
    server backend2.example.com;
}

UPDATE:

If take a look at the Stream module (TCP proxy), the very first example shows exact the same approach:

upstream backend {
    hash $remote_addr consistent;

    server backend1.example.com:12345  weight=5;
    server backend2.example.com:12345;
    server unix:/tmp/backend3;
}

server {
    listen 12346;
    proxy_pass backend;
}
Sprout
  • 630
  • 1
  • 5
  • 22
Anatoly
  • 15,298
  • 5
  • 53
  • 77
  • Sounds like it should work, but I think it does not. We're still hitting the same instance each time, in a local network / subnet. It does not take into account last IPv4 octet. – stamster Jan 24 '17 at 16:27