Nginx, Load Balancer
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.
After that, extract the file that you have just downloaded.
Open the folder with your Text Editor. Search for ‘conf’ folder, then open ‘nginx.conf’.
Search for http protocol, and add an upstream there. You can name the upstream group whatever you want.
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)”
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.
Now, run Nginx by command ‘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’.
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!