I’ve been researching bandwidth limits recently, and it’s just boring. Firstly, I haven’t been attracted to a website that makes people desperately drag data, and secondly, I really don’t know what to play with... The bandwidth limit under Nginx seems to be relatively simple. Here are the two methods I used During the test process between two VPSs, the test machine used was BlueVM’s $9.95 annual payment OpenVZ, which I bought at a low price last month; the one used to drag data was DS2 monthly payment, which I paid a lot of money for a long time ago. Ah, now...there are such prices everywhere!
This article was tested in the Junge LNMP one-click package environment. There are two main modifications.
Modify the nginx default configuration file and use the nginx standard module ngx_http_limit_zone_module to control the number of concurrent connections in the session.

1
2
#cd /usr/local/nginx/conf
#vi nginx.conf

Add the following to http{}:

1
limit_zone one $binary_remote_addr 10m;

[Explanation]We can see a line in nginx.conf as follows:
#limit_zone  crawler  $binary_remote_addr  10m;
At first, I just removed this The # in front of the line uncomments, but an error is reported, crawler must be replaced with one. Regarding the content added in the above line, it mainly defines a recording area called "one" with a total capacity of 10M, and uses the variable $binary_remote_addr as the session judgment benchmark.
For testing, I resolved a domain name and added the domain host to the VPS. Next, we need to modify the second time, which is the domain name configuration file.

1
2
#cd /usr/local/nginx/conf/vhost
#vi domain name.conf

Add a paragraph:

1
2
3
4
location / {
limit_conn one 1;
limit_rate 500k;
}

This paragraph indicates that each client only allows one connection, and the speed limit is 500KB/s.
After all modifications, check the configuration file and reload nginx.

1
2
#/usr/local/nginx/sbin/nginx –t
#service nginx reload

In fact, I encountered a little error here, as shown below, but it didn't seem to affect the effect.
nginx-limit-1
According to the error he reported, I modified nginx.conf, but according to the modification he wrote, the direct configuration file detection failed. If you know how to deal with this, please feel free to do so. Give me some advice.
After finishing it, I put a 100MB test file. The picture below is the effect. The contrast between adding restrictions and not adding restrictions is obvious.
nginx-limit
[Postscript]This restriction has certain uses, such as your own download resource site, but it cannot completely control hot links and the like. Question, we can use its anti-hotlink module such as ngx_http_referer_module, or ngx_http_accesskey_module, etc. We can analyze the log, match $http_user_agent, and then return 503 and so on.

postid
18831

Leave a Reply