How To Set Up Nginx Load Balancing

Published on August 27, 2012
How To Set Up Nginx Load Balancing

About Load Balancing

Loadbalancing is a useful mechanism to distribute incoming traffic around several capable Virtual Private servers.By apportioning the processing mechanism to several machines, redundancy is provided to the application -- ensuring fault tolerance and heightened stability. The Round Robin algorithm for load balancing sends visitors to one of a set of IPs. At its most basic level Round Robin, which is fairly easy to implement, distributes server load without implementing considering more nuanced factors like server response time and the visitors’ geographic region.


The steps in this tutorial require the user to have root privileges on your VPS. You can see how to set that up in the Users Tutorial.

Prior to setting up nginx loadbalancing, you should have nginx installed on your VPS. You can install it quickly with apt-get:

sudo apt-get install nginx

Upstream Module

In order to set up a round robin load balancer, we will need to use the nginx upstream module. We will incorporate the configuration into the nginx settings.

Go ahead and open up your website’s configuration (in my examples I will just work off of the generic default virtual host):

sudo nano /etc/nginx/sites-available/default

We need to add the load balancing configuration to the file.

First we need to include the upstream module which looks like this:

upstream backend  {
  server backend1.example.com;
  server backend2.example.com;
  server backend3.example.com;

We should then reference the module further on in the configuration:

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

Restart nginx:

sudo service nginx restart

As long as you have all of the virtual private servers in place you should now find that the load balancer will begin to distribute the visitors to the linked servers equally.


The previous section covered how to equally distribute load across several virtual servers. However, there are many reasons why this may not be the most efficient way to work with data. There are several directives that we can use to direct site visitors more effectively.


One way to begin to allocate users to servers with more precision is to allocate specific weight to certain machines. Nginx allows us to assign a number specifying the proportion of traffic that should be directed to each server.

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

upstream backend  {
  server backend1.example.com weight=1;
  server backend2.example.com weight=2;
  server backend3.example.com weight=4;

The default weight is 1. With a weight of 2, backend2.example will be sent twice as much traffic as backend1, and backend3, with a weight of 4, will deal with twice as much traffic as backend2 and four times as much as backend 1.


IP hash allows servers to respond to clients according to their IP address, sending visitors back to the same VPS each time they visit (unless that server is down). If a server is known to be inactive, it should be marked as down. All IPs that were supposed to routed to the down server are then directed to an alternate one.

The configuration below provides an example:

upstream backend {
  server   backend1.example.com;
  server   backend2.example.com;
  server   backend3.example.com  down;

Max Fails

According to the default round robin settings, nginx will continue to send data to the virtual private servers, even if the servers are not responding. Max fails can automatically prevent this by rendering unresponsive servers inoperative for a set amount of time.

There are two factors associated with the max fails: 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. Fall_timeout specifies the length of that the server is considered inoperative. Once the time expires, new attempts to reach the server will start up again. The default timeout value is 10 seconds.

A sample configuration might look like this:

upstream backend  {
  server backend1.example.com max_fails=3  fail_timeout=15s;
  server backend2.example.com weight=2;
  server backend3.example.com weight=4;

See More

This has been a short overview of simple Round Robin load balancing. Additionally, there are other ways to speed and optimize a server:

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I followed this tutorial but a blank page appears. Do I have to config in backend servers or make any other configurations. I test with only one backend server. Here is my nginx config in http context

upstream backend { server s1.my_domain.us:80; }

server { listen 80; server_name my_demo_domain.com; location / { proxy_pass http://backend; } }

Please give me some advice,


@marwan, this article is for nginx and not apache - to answer your questions, yes you do have to keep the same set of files on each of the server, and yes, a database (only) server sitting behind it

Is it possible combine least_connect and max fails?

For example:

upstream backend { leastconn; server backend1.example.com maxfails=3 fail_timeout=15s; server backend2.example.com ; server backend3.example.com ;

Do we need to do same NGNIX Config on rest slave servers?

I had my doubts about if Nginx was going to be helpful… and I was having difficulties setting it up. You solved my probelem for setting it up and another helped me realize the benefits of using Nginx for load balancing.

Excellent Tutorial

There are two factors associated with the max fails: max_fails and fall_timeout.

find a typo fall_timeout should be fail_timeout

Hello @kamaln7, I’ve followed this tutorial thoroughly but am having trouble.

In short, when I pass my ip address directly into “proxy_pass”, the proxy works:

server {
        location / {

When I visit my proxy computer, I can see the content from the proxy ip… but when I use an upstream directive, it doesn’t

upstream backend {

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

When I visit my proxy computer, I am greeted with the default Nginx server page.

Any further assistance would be appreciated. I’ve done a ton of research but can’t figure out why “upstream” is not working. I don’t get any errors. It just doesn’t proxy.

This comment has been deleted

    This comment has been deleted

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Featured on Community

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel