Mastering Nginx

(Ron) #1
Chapter 4

[ 75 ]

upstream memcaches {

server 10.0.100.10:11211;

server 10.0.100.20:11211;

keepalive 64;

}

If we were to switch load-balancing algorithms from the default round-robin to


either ip_hash or least_conn, we would need to specify this before using the
keepalive directive:


upstream apaches {

least_conn;

server 10.0.200.10:80;

server 10.0.200.20:80;

keepalive 32;

}

Load-balancing algorithms

The upstream module can select which upstream server to connect to in the next


step by using one of three load-balancing algorithms—round-robin, IP hash, or least
connections. The round-robin algorithm is selected by default, and doesn't need a


configuration directive to activate it. This algorithm selects the next server, based on


which server was selected previously, which server is next in the configuration block,
and what weight each server carries. The round-robin algorithm tries to ensure a fair


distribution of traffic, based on a concept of who's turn it is next.


The IP hash algorithm, activated by the ip_hash directive, instead takes the view that


certain IP addresses should always be mapped to the same upstream server. NGINX
does this by using the first three octets of an IPv4 address or the entire IPv6 address, as


a hashing key. The same pool of IP addresses are therefore always mapped to the same


upstream server. So, this mechanism isn't designed to ensure a fair distribution, but
rather a consistent mapping between the client and upstream server.

Free download pdf