High Availability Website Simulation using Nginx-1.17.6 Load Balancer

In 2018, on the day of the announcement of SNMPTN came, I stumbled upon a news article that says “To prevent the server from crashing, you can access the official SNMPTN website through these websites!” and below the article, there listed some official links from some state universities in Indonesia. I was wondering, how can all of these different links can access the same web entity? And just recently, more than a year later, I discovered that Load Balancing is the answer.

The SNMPTN website I was accessing is one of the examples of high available websites. High availability itself is a term that describes a website or application with maximum potential uptime and accessibility for the content stored on it. While a more basic system will be adequate to serve content to a low or medium number of users, it may include a single point of failure.

Meanwhile, Load balancing refers to a process where we efficiently distribute the incoming network traffic across a group of back end servers, also known as a server farm or server pool. In order to execute load balancing, we need a tool called Load Balancer. Many companies use load balancer in order to handle a high traffic, because one server would not be enough to handle a lot of ‘requests’ from all their clients. Therefore, the company/organization needs to add more servers to make the website more powerful to handle high traffic.

Load balancer works by splitting the traffics from the bandwidths that are being used by different network servers to access the same site evenly, so that each bandwidth could carry the same balanced load.

Now, I am going to try to summarize one of the approaches to implement simple load balancing simulation using one of the many available load balancer engines, Nginx-1.17.6.

First, you can make a Django project and implement TDD (Test Driven Development) for it by configuring the test.py in your App file.

Then, download Nginx from http://nginx.org/en/download.html. I personally chose to download the mainline version, because some sources said it is more reliable than the stable version. After downloading the zip file, you have to extract the content into your Django folder.

After that, open Command Prompt and run it as administrator. Move the directory into your nginx-1.17.6 folder. Then start to run nginx using the command:

start nginx

At this time, the system would try to run nginx service. To find out whether it’s successfully been ran, type command below:

tasklist /fi “imagename eq nginx.exe”

If the output is similar to the block below, your nginx has successfully been ran, and you can access it now from your browser by going to the IP address 127.0.0.1

Next, is to configure the nginx configuration. Make a new configuration file inside the directory conf inside our nginx folder, for example, name it mysite.conf. Add the code below to the file:

upstream web {
server 127.0.0.1:8000;
server 127.0.0.1:9000;
#you can add as many ports as you like
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}

Open nginx.conf, and add the following code just below the line

default_type application/octet-stream;

include mysite.conf

After that, back to the command prompt and reload your nginx using:

nginx -s reload

Next, you can test your load balancer by connecting it into your Django app. To see which server is serving your request, you can add the following element into your HTML code:

{{ host }}:{{ port }}

But before that, you have to configure your views.py so that it has the context to render into your HTML, for instance, you can add the following function:

def index(request):
host = request.get_host()
port = request.get_port()
return render(request, ‘index.html’, {‘host’:host, ‘port’:port})

After you’re done setting everything up, you can test it out by running two or more (depending on how many ports you’ve made) different command prompts and type the command below in each cmd:

python manage.py runserver <port>

Later on, you can now go back to your browser, and open localhost again. It will load your HTML web page and show which port is your webpage served from. And if you take down one server, voila! the webpage can still be displayed, since the other server is still handling your request.

Well, that’s it. That’s how I configured my load balancer and simply simulate it on my own Django project using Nginx-1.17.6. Hope this article can give you insights on the load balancing activity and is useful for all of you my readers. Cheers!

Sources:

a computer science major in university of indonesia.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store