How to set up nginx load balancer

Load balancer is a very useful feature in distributing incoming traffic to several servers. why would you want to do that?

By splitting load or by redirecting visitors to several active machines in a round robin effect, redundancy is achieved. (this ensures users/visitors doesn’t experience downtime on your server) . The Round Robin algorithm for load balancing sends visitors to one of a set of IPs.

A basic Round Robin feature is fairly easy to implement, it distributes server load without the worry of user experiencing server response time and server downtime. A sample below could help you setup a basic load balancing feature.

First this is you need to have nginx installed.

Setup
1. open you website’s nginx conf file

2. include the upstream module which looks like this:

upstream backend  {
  server vps1.example.com;
  server vps2.example.com;
  server vps3.example.com;
}

3. reference the module in the configuration:

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

4. Restart nginx

Load Weight
If you have several server of different specs that will be load balanced, and wanting to distribute the user in proportion. Nginx can assign a number specifying the proportion of traffic that should be directed to each server.

A load balanced setup that included server weight can look like this:

upstream backend  {
  server vps1.example.com weight=1;
  server vps2.example.com weight=2;
  server vps3.example.com weight=4;
}

IP Hash
IP hash allows servers to respond to visitors according to their IP address, then sending visitors back to the same server each time they visit (unless the server is down). If a server is not active it will be marked as down. All users that were supposed to connect to the down server will be then directed to an alternate active/alive servers.

upstream backend {
  ip_hash;
  server   vps1.example.com;
  server   vps2.example.com;
  server   vps3.example.com  down;
 }

Max Fails

Max Fail helps instructs nginx to redirect user to a new server with certain conditions met. In the example below I will be sharing 2 basic directives max_fails and fall_timeout.

max_fails – refers to the maximum number of failed attempts to connect to a server should occur before it is considered inactive.
fail_timeout – sets the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server inactive, By default, the parameter is set to 10 seconds. definitions from http://nginx.org/en/docs/http/ngx_http_upstream_module.html

A sample configuration might look like this:

upstream backend  {
  server vps1.example.com max_fails=3  fail_timeout=15s;
  server vps2.example.com weight=2;
  server vps3.example.com weight=4;
}