Hey, Ryan.
And welcome back.
Now in today's video, we will look into how Nginx can be implemented as a load balancer.
Now one of the great things about Nginx is that Nginx can be configured as a load balancer to improve
your server resource availability as well as efficiency.
So now what will happen?
A user will go ahead and send a request to Nginx, which is acting as a load balancer and Nginx depending
upon the load balancing algorithm that you have configured, will distribute the traffic among multiple
servers.
So this is the basic architecture here.
Now in order to distribute the traffic among multiple back end servers, you can make use of the upstream
block within the Nginx configuration file.
So the upstream block can be used to specify the group of servers for which you want the load balanced
traffic to be distributed.
So for example, you have a upstream block and within the upstream block you specify the IP addresses
where the traffic should be distributed.
So let's assume the IP address of server one is 1077 0.2 and server two is 1077 0.3.
So based on this upstream block, Nginx will go ahead and distribute the traffic accordingly.
Now if you look into the sample configuration here, first thing here you define is the upstream block.
So the name of this upstream block is called as backend.
And within here you have defined two servers.
First one is IP of ten 139 0.3 second has IP of ten 139 0.4 So this is one of the unique things that
you need to do.
And then within the server block, all you have to do is you have to specify a proxy pass.
And within here you have to specify the Http followed by the name of the upstream block here, which
is backend.
So here you have specified backend.
Now we have already discussed about the proxy pass directive.
So the second part is something that we already know.
The new part here is the upstream block, which is very simple to understand.
So let's go ahead and look into it practically.
So for today's video, I have three virtual machines available, and in all of them I have the Nginx
that is installed and up and running.
Among which one of the virtual machine will be acting as a load balancer.
So when we discuss that, it is acting as a load balancer.
It basically has this upstream block configuration file that is available and you have a server one
server zero two.
Both of them basically have a Nginx with a specific index.html file to identify them.
So if I just copy the IP of server zero one, let me paste it here.
It returns server zero one If I copy the IP address of server zero two, it basically returns the server
zero two here.
So similar to the architecture that we are discussing, I have three terminals up.
First one is related to the load balancer virtual machine.
Second associated with server zero, one, third associated with server zero two.
Now let me quickly just show you if I quickly go to user share Nginx.
HTML and if I do a cat on index.html it basically contains this is server zero one and same for server
zero two.
It basically contains this is server zero two now for the load balancer based server.
If I quickly go to etcd Nginx.conf dot d, I only have one configuration available called as load balancer
dot conf.
Now if I quickly open this up, it has a very simple configuration.
You have a upstream block here and then you have a server block.
What this server block is doing, it is doing a proxy pass to backend and the backend is nothing but
the reference to this upstream block here.
Now within here we have specified the private IP address of the server one year and the private IP address
of the server two.
So if I directly make a request here, say ten 139 0.3 it basically says server two and ten 139 0.4
it basically says server zero one So this is the basic configuration here.
Now let's do one thing from the load balancer base Nginx instance.
Let's quickly do a curl on localhost.
So this will basically send the request to the load balancer itself.
So when I do a curl on localhost this time it is loading the server zero two let's do a curl localhost
once more.
This time you see it is loading the server.
Zero one again if I make a request server zero two again, if I make a request server zero one So this
is one of the easiest way in which Nginx can work as a load balancer.
Again, very similar to the curl client.
You can even make a request from the browser.
Let's load the IP address of the load balancer here.
First request to server zero two.
Then you have server zero one again, server zero two again server zero one.
So I hope with this you understood the basics of load balancer and how Enginex can be used as a load
balancer to distribute the traffic among multiple backends.
So with this we'll conclude today's video.