My First Load Balancer

Mushaffa Huda
2 min readMar 19, 2021

--

I usually never used any sort of load balancer for my web projects, but in this spesific task given on my web programming class, we are required to implement in on our django website.

firstly, i already know a bit about load balancing and the usage of it, so it wasn’t really hard for me to get started.

Installation

To install Nginx, on a unix based system, all you have to do is type these commands into the terminal.

sudo apt-get install nginx

there is should be no problem in installation if you just type those commands

Configuration

You need to edit some configuration files to set up the load balancer. The file you need to edit is /etc/nginx/conf.d/ and make add a new .conf file, it would define the port you would like to pass to nginx.

it would look something like this:

upstream bookstore {
server localhost:8000;
server localhost:8001;
server localhost:8002;
}
server {
listen 80;
location /home {
proxy_pass "http://bookstore/home";
}
}

after adding it in the conf.d directory, make sure to include it in the main nginx configuration file on /etc/nginx/nginx.conf, but it should have been included by default.

Restart NginX

Once you have finished editing your configuration, you should restart nginx to see the applied changes

sudo service nginx restart

or

sudo systemctl restart nginx

or on older versions of ubuntu

sudo /etc/init.d/nginx restart

Final Checks

Finally, to test if nginx is working or not, I ran 3 django servers, that is as i have defined on the configuration file, first i open the first terminal and run it on default django port (which is 8000)

python manage.py runserver

And for the second terminal, I ran it on port 8001 by typing:

python manage.py runserver 8001

and also the third one,

python manage.py runserver 8002

Now, you can open the browser and access the load balancer. It should open normally like your django website. But the requests in all of the terminal should be equally distributed.

Conclusion

Finally it’s done! You now have a basic setup for high availability server with nginx for your computer. There are also other tools to achieve this high availabilty for your website in the future, that is both better and more efficient. but we’ll cover it on another time.

{% endblock %}

--

--