Setting Up Nginx As A Load Balancer

One of the most important tasks when creating a scalable application is being able to balance the load of the application. Balancing the load means spreading out the requests along different servers. The more servers that are added, the more requests the application can process at a time. We need a way to add and take away servers quickly and painlessly to do this right. Fortunately, Nginx allows us to do this very easily using a concept called upstreams.

Let's have a look.

For the sake of this demo, we'll assume that we have three Ubuntu servers available to us. The first one we will use as our load balancer, and the other two will be where we deploy our application.

Our first task is to install Nginx on our new server. We can install it by typing the following command:

sudo apt-get install nginx  

Next we need to edit our Nginx configuration to set up the load balancing. To do this we'll need to edit /etc/nginx/sites-available/default and add a new server block that looks like this:

server {  
    server_name mydomain.com;
    location / {
        proxy_pass http://servers;
    }
}

Next we need to add our upstream block. I typically add this to the top of the file.

upstream servers {  
    server server1.mydomain.com;
    server server2.mydomain.com;
}

Finally, we restart Nginx

sudo service nginx restart  

THAT'S IT! As long as you have the applications properly setup on server1.mydomain.com and server2.mydomain.com then everything should work fine. It is important to note that you have to point your domain to the server that is being used for the load balancing.

What is beautiful about this setup is that now adding servers or taking down servers is a breeze. Simply comment out one of the servers and restart Nginx. This also allows for no down time updating of an application in production.

There's a lot more cool things that can be done with this system, but I am going to keep this post short and sweet and save those other topics for another time.

Good Luck.

Frankie