So welcome back to the Knowledge Portal Video series.
Today we are going to look into a very interesting module called as Limit Underscore Con.
So basically it means limit underscore connection.
So this module is specifically useful for servers which are providing download related content.
So if you have a server with a lot of files which people will be downloading, then limit underscore
connection module will be a very useful tool for you.
So let me give you an example.
So let's say this is your server and you have Nginx in it.
Now you have bandwidth of 100 Mbps.
And approximately you expect there are ten users who wants to download a file.
So you have a big file over here.
Maybe a software or maybe a video documentary which around ten users wants to download it.
Now, among the ten users, two of them have 50 Mbps connection.
And rest.
Eight of them has around, let's assume two Mbps connection.
Now, the problem that will happen over here is the two users with 50 Mbps connection.
When they try and download the file, they will be occupying the full 100 Mbps bandwidth of the server.
So rest eight people when they try to download the file.
Either the download will be slow or the server will return 503.
So during these type of scenarios, it is very important to limit the amount of connection speed that
you give to each individual user.
So during this instance of time, let's say you give ten Mbps to each user.
Then essentially all the ten users will be able to download the file.
But if you do not have any restriction on the amount of download speed a user can have, then typically
the people who will have very fast download speed will choke up the whole server.
So let's see on how exactly it works.
So this is one of the CentOS directory.
Well, I'll download a sample CentOS file.
We'll download this 600 MB file.
So I've copied the link and let's try and download it.
So I'll go to temp.
I'll paste it over here.
So before we download that, let's actually test our speed.
So I have this 100 MB test file which basically will help us know the true speed of the server.
So I'll do w get.
And let's see, what is the speed that our server is downloading the file.
Okay.
So the 100 MB file got downloaded and the average speed was around 17.7 Mbps.
So you can consider 18 Mbps download speed.
Now let's take this particular file, CentOS.
Let's copy the link location and let's do a wget and try to download it.
Now, typically, if you see these are the websites who generally get thousands of users at an instant
of maybe a day who will be downloading all of these files.
So they actually cannot give full speed to all the users.
So they actually have a lot of bandwidth based restrictions on the file.
So here you see, I'm able to download the file at around 600 KB, 600 to 700 KB maximum, even though
I can download it more than 17 Mbps.
But the server has a restriction of around 600 KB, so no one will be able to download this particular
file at speed more than 700 or 600 KB.
S.
So this is something which is very important for the servers who are hosting files to download.
So we'll be doing the same thing for our Nginx machine.
And let's see on how we can do that.
So let's go to the Nginx configuration.
And let's create a new location directive called as downloads.
So I'll say location slash downloads.
The route will be via w-w-w.
Websites.
Example.
And I'll close the director.
Let's save this test if the configuration is okay and it is good.
So let's go to var W-w-w example.
Sorry.
Websites.
Example.
Let's create a folder called as downloads.
And inside downloads will be the file which people will be downloading from.
So we have already downloaded a 100 MB 100 M test file, so let's copy this file from temp directory
to this download directory.
And inside download directory we have a 100 MB 100 mb test file.
That's also reload the nginx.
Okay, so inside the download directory we have a 100 MB file.
So let's use some other server to download the particular file.
So this is my second server.
So what I'll do is I'll go to temp directory and let's do a wget on server, a.com/downloads
followed by the name of the file.
Which is 100 and.
Beta test.
So similarly.
The file has been downloaded and the average download speed is 17 Mbps.
So as there is no restriction, the server was able to download it at full speed.
So now let's use the limit, underscore con module and let's put up restrictions.
So let's go to our nginx.
And inside the download location directive.
Beneath the root.
There is a directive called as.
Limit underscore rate, which let's put it as 50 K.
And let's save it
and let's reload the enginex configuration.
So basically what we have told to Nginx is only allow the file to be downloaded at maximum 50 KB s.
So if I try to download the same file again.
You see from 17 Mbps per second to 50 KB per second.
So this is how basically the restriction on download limit is done.
Now lets me show you some more interesting stuff.
So let's connect to server B.
And use.
Good.
Okay, so this is server A from which we are downloading the file and this is the server B from which
the file is getting downloaded.
So what will happen if I download the same file in server B multiple times?
So let me copy this again.
Let's copy this and let's paste it again.
So you see, there are two connections from same IP address and each connection is able to download
the file at 50 KB.
S.
So if you have, say, ten connections, then essentially you can download 50 into ten connections worth
of speed.
So this is generally not ideal.
So we have to limit the amount of speed that we give to each an individual connection.
So we can also do that with Nginx.
So in order to do that, we have to define a zone similar to similarly to the zone that we had defined
earlier for shared memory and buffer.
Let's define one more zone.
So I'll say limit, underscore, connection, underscore zone.
Here it will be binary, remote.
Addr.
The zone name.
Let's put it as ADR.
And the amount of space that we want to give is, say, ten MB.
So what basically this means is the binary remote ADR.
This is an alternative to remote addr.
Basically, this means the IP address of the client who will be downloading the file.
So in this, let's add one more directive
limit, underscore con ADR one.
So basically what this means is for the downloads directory, a particular IP address can only have
one connection at an instant of time.
So if I save this and if I do a reload and let me do a control C on both the instance.
So let's download the file again over here and you see it is getting downloaded.
Now there is already a connection which is established.
Now if I try to download one more file from this same IP address, basically it will give a file three
because there is already one connection on from a particular IP who is downloading the file.
So this is something that you can do on your production machine.
Now, there is one more interesting directive that you should remember is I'll.
And.
Limit underscore rate underscore after.
So what this basically means is first 50 m allow the user to download it at the maximum speed.
After 50 mb, limit the rate to 50 Mbps.
So I'll save this.
Let's reload the nginx.
And.
Let's control C and let's try downloading the file again.
So now you see the first 50 MB.
It downloaded instantaneously after 50 MB.
The speed will be reduced back to 50 KB.
S.
And this is how a lot of websites specifically for if you try to download a file and if you are not
a premium user, you see there is a bandwidth restriction that you get.
Maybe you will be only be able to download file at 100 KB maximum and so on.
So these are some of the things that you can do with the Http limit underscore module.
So I hope this has been informative for you and I'd like to thank you for viewing.