Implement Nginx Load Balancing in Django

Muhammad Zuhdi
2 min readDec 3, 2019

--

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It was created by Igor Sysoev and first publicly released in 2004.

First of all, I will only show you a way to implement load balancing in Django Web Application in ArchLinux OS (one of linux distribution). You can do more research to know more about NGINX. So, before we going any further, we have to make sure that we already made a Django Project. Then, lets follow these steps.

Installing NGINX

To install NGINX in ArchLinux OS, you can run this command in your terminal

sudo pacman -S nginx

Starting NGINX service

To test that you NGINX already installed in your system, you can run this command in your terminal

sudo systemctl start nginx.service

And then, open 127.0.0.1 in your browser. If you have successfully installed NGINX, you will see this in your browser

Page in the browser after starting NGINX

NGINX Configuration (Local)

Now, i will show you the configuration of NGINX to make local load balancer. First, run this command in your terminal

cd /etc/nginx

Then, make a file with .conf as extension. For example, I will make a mysite.conf file in the current folder. If you can’t make a file manually, you can run this comand to make a file

sudo nano <filename>.conf

After that, type this inside your file

Type all of this inside your new file

You can fill <port1> and <port2> with whatever ports that you want for the server that involved in load balancer. While, <listen_port> can you fill with the port that will be used when requesting to the Web App that connected to NGINX.

Open nginx.conf in the current directory and add this line

include <filename>.conf

under this line

default_type application/octet-stream

Then, run this command to restart the nginx

nginx -s reload

Serving Web App

After configuration step, load balancing has been successfully done. To see that the load balancing works properly, you can run these commands in two different terminal

python manage.py runserver 0.0.0.0:<port1>

python manage.py runserver 0.0.0.0:<port2>

And then, open 127.0.0.1:<listen_port> in your browser. See the logs in terminals to see what server that get the request.

Thanks for reading my tutorial. I will be so gratefull if you leave a comment in this post.

--

--

No responses yet