WEBVTT

00:00.080 --> 00:00.560
Hey, Ryan.

00:00.560 --> 00:01.460
And welcome back.

00:01.460 --> 00:07.120
Now in today's video, we will look into how Nginx can be implemented as a load balancer.

00:07.130 --> 00:13.400
Now one of the great things about Nginx is that Nginx can be configured as a load balancer to improve

00:13.400 --> 00:16.610
your server resource availability as well as efficiency.

00:16.610 --> 00:17.810
So now what will happen?

00:17.810 --> 00:24.140
A user will go ahead and send a request to Nginx, which is acting as a load balancer and Nginx depending

00:24.140 --> 00:29.660
upon the load balancing algorithm that you have configured, will distribute the traffic among multiple

00:29.660 --> 00:30.260
servers.

00:30.260 --> 00:32.590
So this is the basic architecture here.

00:32.600 --> 00:38.240
Now in order to distribute the traffic among multiple back end servers, you can make use of the upstream

00:38.240 --> 00:40.640
block within the Nginx configuration file.

00:40.640 --> 00:46.430
So the upstream block can be used to specify the group of servers for which you want the load balanced

00:46.430 --> 00:47.960
traffic to be distributed.

00:47.960 --> 00:53.510
So for example, you have a upstream block and within the upstream block you specify the IP addresses

00:53.510 --> 00:55.550
where the traffic should be distributed.

00:55.550 --> 01:03.480
So let's assume the IP address of server one is 1077 0.2 and server two is 1077 0.3.

01:03.510 --> 01:08.460
So based on this upstream block, Nginx will go ahead and distribute the traffic accordingly.

01:08.460 --> 01:13.680
Now if you look into the sample configuration here, first thing here you define is the upstream block.

01:13.680 --> 01:16.560
So the name of this upstream block is called as backend.

01:16.560 --> 01:19.070
And within here you have defined two servers.

01:19.080 --> 01:27.360
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

01:27.360 --> 01:28.170
you need to do.

01:28.170 --> 01:33.620
And then within the server block, all you have to do is you have to specify a proxy pass.

01:33.630 --> 01:38.610
And within here you have to specify the Http followed by the name of the upstream block here, which

01:38.610 --> 01:39.140
is backend.

01:39.150 --> 01:40.650
So here you have specified backend.

01:40.680 --> 01:43.740
Now we have already discussed about the proxy pass directive.

01:43.740 --> 01:46.830
So the second part is something that we already know.

01:46.860 --> 01:52.320
The new part here is the upstream block, which is very simple to understand.

01:52.320 --> 01:55.140
So let's go ahead and look into it practically.

01:55.140 --> 02:01.350
So for today's video, I have three virtual machines available, and in all of them I have the Nginx

02:01.350 --> 02:03.750
that is installed and up and running.

02:03.780 --> 02:08.100
Among which one of the virtual machine will be acting as a load balancer.

02:08.100 --> 02:11.340
So when we discuss that, it is acting as a load balancer.

02:11.340 --> 02:17.700
It basically has this upstream block configuration file that is available and you have a server one

02:17.700 --> 02:18.810
server zero two.

02:18.810 --> 02:25.320
Both of them basically have a Nginx with a specific index.html file to identify them.

02:25.320 --> 02:29.400
So if I just copy the IP of server zero one, let me paste it here.

02:29.520 --> 02:36.270
It returns server zero one If I copy the IP address of server zero two, it basically returns the server

02:36.270 --> 02:37.080
zero two here.

02:37.080 --> 02:41.100
So similar to the architecture that we are discussing, I have three terminals up.

02:41.100 --> 02:44.460
First one is related to the load balancer virtual machine.

02:44.460 --> 02:48.450
Second associated with server zero, one, third associated with server zero two.

02:48.480 --> 02:53.220
Now let me quickly just show you if I quickly go to user share Nginx.

02:53.430 --> 03:00.870
HTML and if I do a cat on index.html it basically contains this is server zero one and same for server

03:00.870 --> 03:01.320
zero two.

03:01.350 --> 03:07.110
It basically contains this is server zero two now for the load balancer based server.

03:07.470 --> 03:14.910
If I quickly go to etcd Nginx.conf dot d, I only have one configuration available called as load balancer

03:14.910 --> 03:15.930
dot conf.

03:15.960 --> 03:20.910
Now if I quickly open this up, it has a very simple configuration.

03:20.910 --> 03:24.510
You have a upstream block here and then you have a server block.

03:24.510 --> 03:30.450
What this server block is doing, it is doing a proxy pass to backend and the backend is nothing but

03:30.450 --> 03:32.760
the reference to this upstream block here.

03:32.790 --> 03:39.240
Now within here we have specified the private IP address of the server one year and the private IP address

03:39.240 --> 03:40.080
of the server two.

03:40.080 --> 03:51.090
So if I directly make a request here, say ten 139 0.3 it basically says server two and ten 139 0.4

03:51.120 --> 03:55.770
it basically says server zero one So this is the basic configuration here.

03:55.800 --> 03:59.400
Now let's do one thing from the load balancer base Nginx instance.

03:59.400 --> 04:01.890
Let's quickly do a curl on localhost.

04:01.890 --> 04:05.310
So this will basically send the request to the load balancer itself.

04:05.310 --> 04:11.190
So when I do a curl on localhost this time it is loading the server zero two let's do a curl localhost

04:11.190 --> 04:11.910
once more.

04:12.360 --> 04:14.790
This time you see it is loading the server.

04:14.790 --> 04:21.060
Zero one again if I make a request server zero two again, if I make a request server zero one So this

04:21.060 --> 04:24.990
is one of the easiest way in which Nginx can work as a load balancer.

04:24.990 --> 04:27.300
Again, very similar to the curl client.

04:27.300 --> 04:30.390
You can even make a request from the browser.

04:30.390 --> 04:33.810
Let's load the IP address of the load balancer here.

04:33.810 --> 04:36.240
First request to server zero two.

04:36.540 --> 04:41.550
Then you have server zero one again, server zero two again server zero one.

04:41.550 --> 04:47.880
So I hope with this you understood the basics of load balancer and how Enginex can be used as a load

04:47.880 --> 04:51.240
balancer to distribute the traffic among multiple backends.

04:51.240 --> 04:53.700
So with this we'll conclude today's video.
