WEBVTT

00:00.860 --> 00:04.280
So welcome back to the Knowledge Portal Video series.

00:04.760 --> 00:10.310
Today we are going to look into a very interesting module called as Limit Underscore Con.

00:10.520 --> 00:14.330
So basically it means limit underscore connection.

00:14.810 --> 00:25.310
So this module is specifically useful for servers which are providing download related content.

00:25.310 --> 00:32.390
So if you have a server with a lot of files which people will be downloading, then limit underscore

00:32.390 --> 00:36.050
connection module will be a very useful tool for you.

00:36.830 --> 00:39.110
So let me give you an example.

00:39.980 --> 00:44.960
So let's say this is your server and you have Nginx in it.

00:46.550 --> 00:49.370
Now you have bandwidth of 100 Mbps.

00:54.150 --> 01:00.060
And approximately you expect there are ten users who wants to download a file.

01:00.510 --> 01:02.700
So you have a big file over here.

01:03.630 --> 01:09.510
Maybe a software or maybe a video documentary which around ten users wants to download it.

01:10.870 --> 01:17.920
Now, among the ten users, two of them have 50 Mbps connection.

01:20.520 --> 01:21.470
And rest.

01:21.480 --> 01:26.790
Eight of them has around, let's assume two Mbps connection.

01:29.320 --> 01:36.850
Now, the problem that will happen over here is the two users with 50 Mbps connection.

01:36.850 --> 01:43.570
When they try and download the file, they will be occupying the full 100 Mbps bandwidth of the server.

01:44.110 --> 01:50.470
So rest eight people when they try to download the file.

01:50.650 --> 01:55.600
Either the download will be slow or the server will return 503.

01:56.950 --> 02:06.010
So during these type of scenarios, it is very important to limit the amount of connection speed that

02:06.010 --> 02:08.770
you give to each individual user.

02:08.980 --> 02:15.460
So during this instance of time, let's say you give ten Mbps to each user.

02:17.040 --> 02:21.030
Then essentially all the ten users will be able to download the file.

02:22.110 --> 02:28.830
But if you do not have any restriction on the amount of download speed a user can have, then typically

02:28.830 --> 02:33.540
the people who will have very fast download speed will choke up the whole server.

02:35.310 --> 02:38.640
So let's see on how exactly it works.

02:42.870 --> 02:47.120
So this is one of the CentOS directory.

02:47.130 --> 02:49.770
Well, I'll download a sample CentOS file.

02:49.780 --> 02:52.530
We'll download this 600 MB file.

02:54.300 --> 02:59.880
So I've copied the link and let's try and download it.

03:00.480 --> 03:01.860
So I'll go to temp.

03:05.620 --> 03:06.730
I'll paste it over here.

03:09.070 --> 03:12.460
So before we download that, let's actually test our speed.

03:13.120 --> 03:21.910
So I have this 100 MB test file which basically will help us know the true speed of the server.

03:22.150 --> 03:24.220
So I'll do w get.

03:25.780 --> 03:29.860
And let's see, what is the speed that our server is downloading the file.

03:32.920 --> 03:33.370
Okay.

03:33.370 --> 03:40.040
So the 100 MB file got downloaded and the average speed was around 17.7 Mbps.

03:40.060 --> 03:43.210
So you can consider 18 Mbps download speed.

03:44.140 --> 03:51.640
Now let's take this particular file, CentOS.

03:52.120 --> 03:59.650
Let's copy the link location and let's do a wget and try to download it.

04:03.050 --> 04:11.390
Now, typically, if you see these are the websites who generally get thousands of users at an instant

04:11.390 --> 04:15.470
of maybe a day who will be downloading all of these files.

04:15.470 --> 04:20.090
So they actually cannot give full speed to all the users.

04:20.090 --> 04:26.540
So they actually have a lot of bandwidth based restrictions on the file.

04:27.260 --> 04:36.230
So here you see, I'm able to download the file at around 600 KB, 600 to 700 KB maximum, even though

04:36.230 --> 04:40.790
I can download it more than 17 Mbps.

04:40.790 --> 04:49.100
But the server has a restriction of around 600 KB, so no one will be able to download this particular

04:49.100 --> 04:53.470
file at speed more than 700 or 600 KB.

04:53.510 --> 04:53.810
S.

04:53.930 --> 04:59.960
So this is something which is very important for the servers who are hosting files to download.

05:01.640 --> 05:07.380
So we'll be doing the same thing for our Nginx machine.

05:07.380 --> 05:09.840
And let's see on how we can do that.

05:13.970 --> 05:18.110
So let's go to the Nginx configuration.

05:30.180 --> 05:36.000
And let's create a new location directive called as downloads.

05:36.000 --> 05:40.500
So I'll say location slash downloads.

05:44.560 --> 05:47.230
The route will be via w-w-w.

05:47.350 --> 05:48.880
Websites.

05:51.260 --> 05:51.950
Example.

05:56.620 --> 05:58.570
And I'll close the director.

06:00.960 --> 06:06.300
Let's save this test if the configuration is okay and it is good.

06:06.510 --> 06:10.950
So let's go to var W-w-w example.

06:11.820 --> 06:12.090
Sorry.

06:12.090 --> 06:13.230
Websites.

06:13.350 --> 06:14.310
Example.

06:15.000 --> 06:17.190
Let's create a folder called as downloads.

06:23.190 --> 06:28.620
And inside downloads will be the file which people will be downloading from.

06:28.620 --> 06:37.140
So we have already downloaded a 100 MB 100 M test file, so let's copy this file from temp directory

06:37.950 --> 06:39.810
to this download directory.

06:50.180 --> 06:54.860
And inside download directory we have a 100 MB 100 mb test file.

06:56.780 --> 06:59.150
That's also reload the nginx.

07:01.090 --> 07:06.720
Okay, so inside the download directory we have a 100 MB file.

07:06.730 --> 07:11.960
So let's use some other server to download the particular file.

07:11.980 --> 07:14.490
So this is my second server.

07:14.500 --> 07:24.940
So what I'll do is I'll go to temp directory and let's do a wget on server, a.com/downloads

07:26.470 --> 07:28.360
followed by the name of the file.

07:30.350 --> 07:33.130
Which is 100 and.

07:35.660 --> 07:36.890
Beta test.

07:42.530 --> 07:44.690
So similarly.

07:47.350 --> 07:52.660
The file has been downloaded and the average download speed is 17 Mbps.

07:52.810 --> 07:59.560
So as there is no restriction, the server was able to download it at full speed.

07:59.590 --> 08:05.500
So now let's use the limit, underscore con module and let's put up restrictions.

08:07.330 --> 08:11.350
So let's go to our nginx.

08:15.240 --> 08:17.940
And inside the download location directive.

08:21.830 --> 08:22.910
Beneath the root.

08:26.730 --> 08:28.440
There is a directive called as.

08:32.800 --> 08:38.890
Limit underscore rate, which let's put it as 50 K.

08:43.560 --> 08:44.700
And let's save it

08:47.160 --> 08:50.190
and let's reload the enginex configuration.

08:52.510 --> 09:01.240
So basically what we have told to Nginx is only allow the file to be downloaded at maximum 50 KB s.

09:01.780 --> 09:05.170
So if I try to download the same file again.

09:09.050 --> 09:16.620
You see from 17 Mbps per second to 50 KB per second.

09:16.640 --> 09:22.130
So this is how basically the restriction on download limit is done.

09:26.280 --> 09:31.140
Now lets me show you some more interesting stuff.

09:31.140 --> 09:34.440
So let's connect to server B.

09:39.710 --> 09:40.190
And use.

09:40.190 --> 09:40.490
Good.

09:45.640 --> 09:53.410
Okay, so this is server A from which we are downloading the file and this is the server B from which

09:53.410 --> 09:55.060
the file is getting downloaded.

09:55.180 --> 10:03.430
So what will happen if I download the same file in server B multiple times?

10:11.170 --> 10:13.120
So let me copy this again.

10:14.350 --> 10:18.070
Let's copy this and let's paste it again.

10:22.070 --> 10:29.870
So you see, there are two connections from same IP address and each connection is able to download

10:30.230 --> 10:32.540
the file at 50 KB.

10:32.540 --> 10:33.050
S.

10:34.340 --> 10:43.280
So if you have, say, ten connections, then essentially you can download 50 into ten connections worth

10:43.280 --> 10:44.120
of speed.

10:44.510 --> 10:47.040
So this is generally not ideal.

10:47.060 --> 10:54.860
So we have to limit the amount of speed that we give to each an individual connection.

10:54.950 --> 10:59.000
So we can also do that with Nginx.

11:01.790 --> 11:08.960
So in order to do that, we have to define a zone similar to similarly to the zone that we had defined

11:08.960 --> 11:12.050
earlier for shared memory and buffer.

11:14.420 --> 11:16.100
Let's define one more zone.

11:16.340 --> 11:20.210
So I'll say limit, underscore, connection, underscore zone.

11:21.200 --> 11:25.640
Here it will be binary, remote.

11:26.540 --> 11:27.260
Addr.

11:31.280 --> 11:32.150
The zone name.

11:32.450 --> 11:34.430
Let's put it as ADR.

11:35.000 --> 11:39.530
And the amount of space that we want to give is, say, ten MB.

11:41.560 --> 11:46.600
So what basically this means is the binary remote ADR.

11:46.720 --> 11:50.350
This is an alternative to remote addr.

11:50.380 --> 11:55.720
Basically, this means the IP address of the client who will be downloading the file.

11:57.480 --> 12:03.930
So in this, let's add one more directive

12:06.270 --> 12:11.490
limit, underscore con ADR one.

12:11.700 --> 12:22.320
So basically what this means is for the downloads directory, a particular IP address can only have

12:22.320 --> 12:24.780
one connection at an instant of time.

12:25.800 --> 12:37.260
So if I save this and if I do a reload and let me do a control C on both the instance.

12:37.560 --> 12:43.380
So let's download the file again over here and you see it is getting downloaded.

12:43.740 --> 12:46.740
Now there is already a connection which is established.

12:46.770 --> 12:53.850
Now if I try to download one more file from this same IP address, basically it will give a file three

12:53.880 --> 13:00.310
because there is already one connection on from a particular IP who is downloading the file.

13:01.030 --> 13:07.270
So this is something that you can do on your production machine.

13:10.810 --> 13:18.290
Now, there is one more interesting directive that you should remember is I'll.

13:20.970 --> 13:21.160
And.

13:24.620 --> 13:27.230
Limit underscore rate underscore after.

13:29.030 --> 13:36.830
So what this basically means is first 50 m allow the user to download it at the maximum speed.

13:37.460 --> 13:41.930
After 50 mb, limit the rate to 50 Mbps.

13:42.830 --> 13:44.600
So I'll save this.

13:45.110 --> 13:47.180
Let's reload the nginx.

13:48.830 --> 13:49.520
And.

13:50.430 --> 13:54.330
Let's control C and let's try downloading the file again.

13:55.020 --> 13:58.020
So now you see the first 50 MB.

13:58.620 --> 14:02.790
It downloaded instantaneously after 50 MB.

14:03.330 --> 14:06.300
The speed will be reduced back to 50 KB.

14:06.300 --> 14:06.840
S.

14:07.560 --> 14:15.240
And this is how a lot of websites specifically for if you try to download a file and if you are not

14:15.240 --> 14:19.290
a premium user, you see there is a bandwidth restriction that you get.

14:19.410 --> 14:24.810
Maybe you will be only be able to download file at 100 KB maximum and so on.

14:25.200 --> 14:31.650
So these are some of the things that you can do with the Http limit underscore module.

14:32.100 --> 14:37.710
So I hope this has been informative for you and I'd like to thank you for viewing.
