Nginx, Load Balancer

Michael Susanto
3 min readSep 1, 2020

--

Have you ever thought about how many traffics there in one big web application such as Medium, Facebook, etc?

Have you ever thought how they handle such massive traffics?

Today, I will share my experience on how did I handle such traffics on my website using Nginx. I will only show you on how does Load Balancer with Nginx work on local machine. I will share my experience with Load Balancer on deploying later.

First of all, you need to download Nginx from their website here. Choose your version. I chose nginx/Windows-1.17.6. For other Operating System, please check their manuals.

Download Nginx

After that, extract the file that you have just downloaded.

Extract Nginx

Open the folder with your Text Editor. Search for ‘conf’ folder, then open ‘nginx.conf’.

Opening ‘nginx.conf’ in Text Editor.

Search for http protocol, and add an upstream there. You can name the upstream group whatever you want.

Define upstreams

So, what is upstream? Upstream defines a cluster that you can proxy requests to. It’s commonly used for defining either a web server cluster for load balancing, or an app server cluster for routing / load balancing. In simple way, upstream defines a group of server that will be used by Nginx to divide traffics. So, if you have 2 servers, Nginx will divide traffics to 50% on 1st server and 50% on 2nd server. That’s why Facebook or Medium can handle such massive traffics even in one second!

Make sure you set protocol and address of your web application by “proxy_pass”-ing to your named upstream group before (proxied server). This means like “all requests for / go to the any of the servers listed under upstream ‘backend’ (my upstream name)”

Set proxy_pass to upstream servers

Don’t forget to give permission for any host servers of your web application. In my case, I use django. So, I set the ALLOWED_HOST to ‘*’, so any web server host will be allowed to serve my app.

Set allowed hosts for my application

Now, run Nginx by command ‘start nginx’.

Start Nginx

Finally, for testing, you can run your server with specific URLs that have been described in ‘nginx.conf’ upstream servers. In my case, I only changed the ports on my URLs in upstream servers (by default, Django only allows port 8000 on local machine). So, I can test my web application on local machine with all ports that I have described in ‘nginx.conf’.

Testing my Web Application on Port 9000
Testing my Web Application on Port 1000

Finally, if you open your main URL, Nginx will choose the web server automatically based on traffics to keep your web application steady and balance!

Testing my Web Application on main URL

--

--

Michael Susanto
Michael Susanto

Written by Michael Susanto

Currently studying at Faculty of Computer Science, Universitas Indonesia.

No responses yet