How to Run Load Balancing on Local Host using NGINX
Why we must use load balancer for your website?
load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers.
For websites that have a large scale, they often have many servers. maybe you will have problem for your website because of this.
“ Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool.”
In this article, I want to give a tutorial about how to do simulation for load balancing using nginx.
1. Download NGINX for Windows and Extract the Zip File
You can simply access the NGINX website to download.
2. Start NGINX in your directory file using comand line
To find out if NGINX can be run, open the Command Prompt at the location of the NGINX folder and then run the command:
start nginx
If you access http://localhost you might see this page:
To stop NGINX you can use this command:
taskkill /f /im nginx.exe
Now try your localhost port for your own website.
3. Edit nginx configuration on your nginx file
to edit configuration you can access:
F:\Semester3\PPW\nginx-1.17.6\conf\nginx.conf
and edit upstream and server in your config nginx file like this
http {
upstream uptoyou{
#ip_hash;
server localhost:1234;
server localhost:8000;
server localhost:1222;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://uptoyou;
}
}
}
I set 3 ports: 8000, 1234 and 1222.
If you change its configuration after start NGINX, you may have to reload the NGINX using this command:
nginx -s reload
4. For the test, Run your Django project on two different ports (8000 and 1234) from different Command Prompt.
After you run your Django project on two different ports, open http://localhost to see if NGINX can run properly.
If your page can be loaded successfully, congratulations! You can check both Command Prompt to see the distribution of your request to different servers equally.
Nginx resources site has a full list of web server applications which you can use to run various applications on your PC.