[Хд] logo

Load balancing with Nginx

Load balancing between multiple applications, backends and servers is a major part of the optimization process, improvement of service’s performance and fault tolerance. Nginx load balancing

Ngin is considered to be one of the most popular and productive solutions, as it has the broadest functionality and the configuration flexibility. So it is often used for load balancing.

There is a couple of approaches and implementations but first check for ngx_http_upstream_module module:

nginx -v

# The output will contain all installed and excluded modules

You will have to recompile/reinstall Nginx, adding the module if it’s missing.

Then you can proceed to set up. To enable balancing feature add upstream directive (http section) in the configuration file:

upstream backend  {
  server backend1.somesite.com;
  server backend2.somesite.com;
  server backend3.somesite.com;
}

# You can assign several upstream directives

Now you need to specify the necessary redirection:

server {
  location / {
    proxy_pass  http://backend;
  }
}

# Redirection can also be specified in fastcgi_pass, memcached_pass, uwsgi_pass, scgi_pass directives

Besides Nginx supports additional parameters and load balancing methods.

Choosing balancing method

Nginx offers several load balancing methods. Nginx least_conn

Round-robin

This is a default web-server method — it distributes requests evenly between the backend (weights are taken into account), so that there is no inclusion directive.

Least_conn

Requests are sent to backend with the lowest number of active connections (weights are taken into account):

upstream backend {
    least_conn;

    server backend1.somesite.com;
    server backend2.somesite.com;
}

# If backends have even number of connections, round-robin is used between them

Hash and IP hash

With this method, you can create a kind of permanent connection between the client and the backend. Nginx hash

Nginx calculates a hash for each request. It can consist of text, a web server variables, or combinations of both, and then compares it with a backend:

upstream backend {
   hash $scheme$request_uri;

   server backend1.somesite.com;
   server backend2.somesite.com;
   server backend3.somesite.com;
}

# Hash calculation uses a scheme (http or https) and the full URL

IP hash only works with HTTP, it’s a predefined embodiment in which the hash is computed of the client’s IP-address:

upstream backend {
   ip_hash;

   server backend1.somesite.com;
   server backend2.somesite.com;
   server backend3.somesite.com;
}

# If client has the IPv4, then hash uses the first three octets, if the IPv6, then the entire address

Server weights

Nginx backends weight

If stack contains more powerful backends than others, weights can be useful:

upstream backend {
    server backend1.somesite.com weight=10;
    server backend2.somesite.com weight=5;
    server backend3.somesite.com;
    server 192.0.0.1 backup;
}

# Weights=1 by default

In this example, out of every 16 requests, the first backend will handle 10, the second 5 and the third 1. The backup server will receive requests only if three major backends are unavailable.

Monitoring

If Nginx considers the backend server is unavailable, then it temporarily stops sending requests to this server. There are two directives to configure:

  • max_fails — specifies the number of failed connection attempts after which a certain backend is considered inaccessible;
  • fail_timeout — the time during which the server is considered unavailable.

The parameters are as follows:

upstream backend {                
    server backend1.somesite.com;
    server backend2.somesite.com max_fails=3 fail_timeout=30s;
    server backend3.somesite.com max_fails=2;
}

# Default settings: 1 attempt and 10 seconds timeout

The most important

Appropriate method will enable a more evenly distributed balancing. Do not forget about the server weights, monitoring, and fault tolerance.

  читать на русском

еще материалы по теме

  • DNS load balancing
  • Load balancing with Nginx
  • [Хд]

    Sign up to read high quality stuff on advanced development

    Google Email

    Esc for later