NginX: Load Balancing, Failover, and Geo Location (Part 1)

Yeah guys, let’s talk about this post title, really it is interesting, i mean for me this is very interesting :p. If you have a site, and then something happen to your server’s network, and unfortunately your site does not have backup or mirror site which is very essential because your site is your income. So the first thing you need to do is this, do not put your site in shared / reseller hosting. Why? Because in shared hosting, your site is not alone, there are so much neighbor in it, and your site could be exploited from those neighbor, for instance bug in script. Another thing is you cannot get root access from shared hosting, your creativity is limited by non-ssh regular user assigned to your account. So take a look at LowEndBox and get a vps for your shared hosting substitution.

Nuff said for the appetizer, let’s get to the main course. The simplest failover method for website is round robin dns, the illustration would be like this:

where the server has been assigned with two or more ip addresses, so if end user fail to access the server with the first ip address, it will be routed to the second ip address. To use that settings, you simply

  • Install DNS in your server and create nameservers with your ip addresses

    ns1.domain.com 212.121.212.121
    ns2.domain.com 212.121.212.122

    after that assign the nameservers to your domain in your domain panel.

  • If you don’t want to use DNS, you can add your ip addresses directly in your domain panel by creating A Record for each ip, for instance:

    A Record domain.com 212.121.212.121 100ms
    A Record domain.com 212.121.212.122 100ms

    but not all domain panels support two or more of the same hostname, so you can do research for supported domain panel or using DNS.

The illustration below is as same as the failover method above, but this is more reliable because using an additional / separated server for live backup.

If the network or assigned ip address in server a is troubled or have an issue, then it will automatically routed to the second server’s network. The setting for ip addresses is same as the above, add the first server’s ip address to your domain A Record and same thing for the second server’s ip address. The files within both server is synchronized one each other with a software like rsync. Here is the way to install and run rsync.

wget http://pkgs.serversreview.net/files/rsync-3.0.9.tar.gz
tar -zxvf rsync-3.0.9.tar.gz
./configure
make
make install

Here’s one example of using rsync, you want to synchronize all of your files inside your public_html directory to the second server, and the public_html directory is located in /home/www/serversreview.net/public_html/, so here is the command

rsync -avz /home/www/serversreview.net/public_html/ user@second_servers_ip_or_host:/home/www/serversreview.net/public_html

now take a look at below command

rsync -avz /home/www/serversreview.net/public_html user@second_servers_ip_or_host:/home/www/serversreview.net

the command is almost the same with the first command, but the second command is syncronizing / copying public_html directory and its content to the path /home/www/serversreview.net of the second server.

I usually use rsync with cron daemon, the command will run either every x minute or every x hour depending on my needs, for more information about commands and manuals you can visit rsync documentation page.

The advantage of using this trick is you do not need to spend large money, especially for the first method, you only need to spend about one or two more dollars to get the second ip address assigned to your server, or double your server cost by adding one more if you want it to be more reliable. Also the setting is not too complicated, just add the nameserver / ip and install rsync. If there’s advantage, so there will be disadvantage, the dns failover depends on the end user’s dns, if end user’s dns is good then the dns shifting from the dead ip to the live ip will not be taking a long time, resolving the dns will be fast and smooth, otherwise if there’s a problem with end user’s dns, then the shifting will take more time, especially if you are implementing the second way and your both servers are located in two different areas or datacenters.

So that’s it for the simple failover (dns and static only files), we will continue later with the more advance failover. See Part 2 Here.

{ 0 comments }

9 thoughts on “NginX: Load Balancing, Failover, and Geo Location (Part 1)

  1. Hi.~ I have question. rsync sync to home directory? is right? If I sync mysql dababase(wordpress). How can I use rsync?

    1. yep, the example i give is for home directory (which is usually the location of public_html). if you want to synchronize mysql, i suggest you to not using rsync because rsync only good to serve static files (files that don’t change). so for mysql you can use wget or curl manually with cronjob, or you can use mysql replication. check the second and third part of this article.

  2. hi there the explanation was awsome, i just wonder if you’re up for hire for some configuration job related to nginX load balance for multiple node for DDOS protection ?

    1. hello,

      thank you for your great offer, but it seems that i’m quite busy at the moment, there are a few jobs need to be done, perhaps i will contact you when i’m available.

    1. hi,

      actually i haven’t actually implemented the load balancing yet (just testing it) because you know serversreview is just a small blog and no need for load balancing, but for geo location based yes i’ve tried that and works, i’m in my way implementing that in my nginx auto installer script.

  3. Hi Bay,

    i’m planning to create something like leecher, do we need high spec on vps thats acting as load balancer?

    and how do we know how much max request for vps (bottleneck?) could handle?

    Thank

    :):):):):):):)

    1. Hi fer,

      i still don’t get about what do you want to create, there are a few type of load balancer, it’s dns, files, database, geo location, etc…

      what kind of leecher do you wanna build with load balancer? and i thought you don’t play with leecher anymore? anything new? tell me.

Leave a Reply

Your email address will not be published. Required fields are marked *