In this tutorial we will cover the steps needed to configure load balancing for Nginx on your CentOS 6.4, Debian or Ubuntu platform.


  • CentOS 6.4, Debian or Ubuntu installed on your computer/server
  • SSH access (Command line access to the server)
  • root privileges
  • Basic skills for working on a Linux environment
  • Nginx/LEMP installed on the server

With the load balancing mechanism provided by Nginx we can distribute the incoming traffic around two or more servers (VPSs or dedicated machines). This way we can achieve fault insurance and better server stability. The load balancing mechanism uses Round robin mechanism to forward the requests to the corresponding server included inside the configuration depending on specific priority directives.

Configure the load balancing

In order for the load balancing mechanism to work correctly the Nginx requires the upstream module included inside the Nginx vhost file:

CentOS 6.4:


where server_array is the name of the server group which will be used for the load balancing configuration.

Inside the same file we can "tell" the virtual host to use the server group:

Once ready we can restart the Nginx service:

Load balancing directives

One of the things that we can do in order to specify the proportion of the distributed traffic is to set a weight directive for the each server included inside the upstream module:

The default wight is 1, will receive twice as much traffic in comparison to and will receive amount of traffic four times larger than the one received by

Another configuration can be setup using a number of maximum fail attempts in communication with the server within specific period of time:

Once the time expires, new attempts to reach the server will start up again.

Another directive called ip_hash causes requests to be distributed between upstreams based on the IP-address of the client:

This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server.

Full list of the available directives can be found on the Nginx official website.