There is an old project, through squid to provide file download function, the use of delay_parameters to achieve bandwidth control, the problem is that I do not turn Squid, and then calculate whether the Nginx can find similar functions.
The good news is that Nginx provides limit_rate and Limit_rate_after, for example, to illustrate:
Copy Code code as follows:
location/download/{
Limit_rate_after 500k;
Limit_rate 50k;
}
The meaning is: User download reaches 500k, then control its speed within 50k.
The bad news is that this control is for a single connection. In other words, you can limit the bandwidth of a single connection and not the total bandwidth. However, with the Limit_conn module, you can mitigate the problem to some extent:
Copy Code code as follows:
Limit_conn_zone $server _name zone=servers:10m;
server {
location/download/{
Limit_conn servers 1000;
Limit_rate_after 500k;
Limit_rate 50k;
}
}
The number of concurrent connections is limited by limit_conn, thus limiting the total bandwidth. Unfortunately, this solution is not perfect, you can imagine the following example: 1000 users can be downloaded at the same time at 50k, then in the total bandwidth unchanged, 2000 users can simultaneously download at 25k speed? From a business point of view, the answer is naturally positive, but in fact Limit_conn and limit_rate is not flexible enough to simply implement such logic.
Of course, there must be a solution to the problem. For example, using a third-party module: limit_speed, you can also use the Linux built-in TC command. Limit_speed more simple, not to say, next we will see the use of TC bar:
Copy Code code as follows:
Shell> tc Qdisc Add dev eth0 root handle 1:HTB default 10
Shell> TC class Add dev eth0 parent 1:classid 1:1 HTB rate 10MBit
Shell> TC Filter Add dev eth0 protocol IP parent 1:0 prio 1 \
U32 match IP dport 0xffff flowid 1:1
TC complex maddening, please refer to: Linux Advanced Routing & Traffic Control HOWTO.
This article introduced a number of Nginx restricted access module, in fact, there is a limit_req module is also super, although the relationship with this article is not small, but we recommend that you can refer to the specific "nginx limit_req speed limit settings.