官方解释
Syntax: | limit_rate |
---|---|
Default: | |
Context: | http , server , location , if in location |
Limits the rate of response transmission to a client. The rate
is specified in bytes per second. The zero value disables rate limiting. The limit is set per a request, and so if a client simultaneously opens two connections, the overall rate will be twice as much as the specified limit.
Parameter value can contain variables (1.17.0). It may be useful in cases where rate should be limited depending on a certain condition:
map $slow $rate { 1 4k; 2 8k; } limit_rate $rate;
Rate limit can also be set in the $limit_rate
variable, however, since version 1.17.0, this method is not recommended:
server { if ($slow) { set $limit_rate 4k; } ... }
Rate limit can also be set in the “X-Accel-Limit-Rate” header field of a proxied server response. This capability can be disabled using the proxy_ignore_headers, fastcgi_ignore_headers, uwsgi_ignore_headers, and scgi_ignore_headers directives.
Syntax: | limit_rate_after |
---|---|
Default: | |
Context: | http , server , location , if in location |
This directive appeared in version 0.8.0.
Sets the initial amount after which the further transmission of a response to a client will be rate limited. Parameter value can contain variables (1.17.0).
Example:
location /flv/ { flv; limit_rate_after 500k; limit_rate 50k; }
Limiting Bandwidth for Particular URLs(流量限速)
If your server provides larger files (or smaller but extremely popular files, like forms or reports), it can be useful to set the maximum speed at which clients can download them. If your site is already experiencing a high network load, limiting download speed leaves more bandwidth to keep critical parts of your application responsive. This is a very popular solution used by hardware manufacturers – you may wait longer to download a 3-GB driver for your printer, but with thousands of other people downloading at the same time you’ll still able to get your download. 😉
如果服务器提供大文件(或者体积小但非常流行的文件,如表单或报表),那么设置客户端下载这些文件的最大速度是很有用的。如果您的站点已经经历了高网络负载,那么限制下载速度会留下更多带宽来保持应用程序的关键部分响应。这是硬件制造商使用的一种非常流行的解决方案-您可能需要更长的时间来下载您的打印机的3GB驱动程序,但是在成千上万的其他人同时下载的情况下,您仍然可以获得您的下载。
Use the limit_rate
directive to limit bandwidth for a particular URL. Here we’re limiting the transfer rate for each file under /download to 50 KB per second.
使用limit_rate指令限制特定URL的带宽。在这里,我们将下载下的每个文件的传输速率限制为每秒50kb。
如果网站提供有较大文件的下载,为防止一个请求占用过多的带宽而影响其他人的访问,可以对其进行限速。
location /download/ {
limit_rate 50k;
}
You might also want to rate‑limit only larger files, which you can do with the limit_rate_after
directive. In this example the first 500 KB of every file (from any directory) is transferred without speed restrictions, with everything after that capped at 50 KB/s. This enables faster delivery of critical parts of the website while slowing down others.
可能你还希望只对较大的文件进行速率限制,这可以通过使用limit_rate_after指令来实现。在本例中,每个文件(来自任何目录)500kb的传输没有速度限制,之后的所有内容都以50kb/s为上限。这样可以更快地传递网站的关键部分,同时降低其他部分的速度。
location / {
limit_rate_after 500k;
limit_rate 50k;
}
Note that rate limits apply to individual HTTP connections between a browser and NGINX, and so don’t prevent users from getting around rate limits by using download managers.
注意,速率限制适用于浏览器和NGINX之间的单个HTTP连接,因此不会阻止用户使用下载管理器绕过速率限制。
Lastly, you can also limit the number of concurrent connections to your server or the rate of request.
最后,还可以限制到服务器的并发连接数或请求速率。
limit_rate || limit_rate_after
因为我们的公网带宽是十分有限的,当有许多并发用户使用我们的带宽时,这样会形成争抢关系,可能会让某些用户访问大文件的时候要限制他的速度。
Nginx的http核心模块ngx_http_core_module中提供limit_rate这个指令可以用于控制速度,limit_rate_after用于设置http请求传输多少字节后开始限速。限制速度的配置指令简单易懂,限速支持固定的数值
名称 | 默认配置 | 作用域 | 官方说明 | 中文解读 | 模块 |
---|---|---|---|---|---|
limit_rate | limit_rate 0; | http, server, location, if in location | Limits the rate of response transmission to a client. The rate is specified in bytes per second. The zero value disables rate limiting. The limit is set per a request, and so if a client simultaneously opens two connections, the overall rate will be twice as much as the specified limit. | 指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽 | ngx_http_core_module |
limit_rate_after | limit_rate_after 0; | http, server, location, if in location | Sets the initial amount after which the further transmission of a response to a client will be rate limited. | 设置多少bytes过后将启动limit计数,如果小于此值则不限速 | ngx_http_core_module |
实例
location /downloads {
limit_rate_after 1m;
limit_rate 500k;
}
limit_rate开启nginx限速功能,可配置在http、server、location和if in location配置段。 limit_rate 500k表示限速500kB每秒,限速对象是单个连接,因此如果一个IP有多个连接的话,每个连接都是限速500k。limit_rate还有在特定情况下开启限速的功能。
limit_rate_after和 limit_rate配合使用表示在下载的文件大小达到设定数后开启限速效果(逐渐降速)。同样针对于单个连接。设定大小设置太小的话可能效果不准确。