Nginx模块 ngx_http_limit_req_module 限制请求速率

The ngx_http_limit_req_module module (0.7.21) is used to limit the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address. The limitation is done using the “leaky bucket” method.

ngx_http_limit_req_module 模块用于限制对每个定义键的请求处理速率,例如,单客户端IP的每秒请求数。实现的原理是使用“漏桶”原理。

Example Configuration:

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

    ...

    server {

        ...

        location /search/ {
            limit_req zone=one burst=5;
        }

Directives指令:

Syntax:	limit_req zone=name [burst=number] [nodelay];
Default:	—
Context:	http, server, location

Sets the shared memory zone and the maximum burst size of requests. If the requests rate exceeds the rate configured for a zone, their processing is delayed such that requests are processed at a defined rate. Excessive requests are delayed until their number exceeds the maximum burst size in which case the request is terminated with an error 503 (Service Temporarily Unavailable). By default, the maximum burst size is equal to zero. For example, the directives
设置共享内存区,以及请求最大数。当请求的速率大于配置的速率,那么这些请求将会被延迟处理。如果,有过多的请求被延迟,超过了最大的限制,服务器将返回503状态码。默认情况下,最大限制为0。例如,以下指令配置:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

server {
    location /search/ {
        limit_req zone=one burst=5;
    }
allow not more than 1 request per second at an average, with bursts not exceeding 5 requests.
允许1秒钟不超过1个请求,最大延迟请求数量不大于5.

If delaying of excessive requests while requests are being limited is not desired, the parameter nodelay should be used:
如果请求不需要被延迟,添加nodelay参数,服务器会立刻返回503状态码。

limit_req zone=one burst=5 nodelay;
There could be several limit_req directives. For example, the following configuration will limit the processing rate of requests coming from a single IP address and, at the same time, the request processing rate by the virtual server:
这里列出以下limit_req的指令。例如,接下来的配置,会对当前站点单个IP的访问速率进行限制:

limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;

server {
    ...
    limit_req zone=perip burst=5 nodelay;
    limit_req zone=perserver burst=10;
}
These directives are inherited from the previous level if and only if there are no limit_req directives on the current level.
这些指令作用继承上级,如果当前级别中没有配置,会继承上一级的指令。

Syntax:	limit_req_log_level info | notice | warn | error;
Default:	
limit_req_log_level error;
Context:	http, server, location
This directive appeared in version 0.8.18.
Sets the desired logging level for cases when the server refuses to process requests due to rate exceeding, or delays request processing. Logging level for delays is one point less than for refusals; for example, if “limit_req_log_level notice” is specified, delays are logged with the info level.
设置服务器拒绝请求速率,或者延迟处理的情况下的日志级别。对于延迟的请求来说,日志的级别比拒绝请求要低。例如,“limit_req_log_level notice” 是通知级别,那么,延迟请求只是信息的日志级别。

Syntax:	limit_req_status code;
Default:	
limit_req_status 503;
Context:	http, server, location
This directive appeared in version 1.3.15.
Sets the status code to return in response to rejected requests.
设置拒绝请求时候的http返回状态码。

Syntax:	limit_req_zone key zone=name:size rate=rate;
Default:	—
Context:	http
Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state stores the current number of excessive requests. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.
为各种定义的键配置共享内存大小,包括当前连接数。key可以是文本、变量、也可以是文本与变量结合,对于空的key请求,不会进行计数。

Usage example:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
Here, the states are kept in a 10 megabyte zone “one”, and an average request processing rate for this zone cannot exceed 1 request per second.
以上的配置,设置了名为“one”的存储区,大小为10兆字节,请求速率为每个客户端IP每秒1个请求。

A client IP address serves as a key. Note that instead of $remote_addr, the $binary_remote_addr variable is used here, that allows decreasing the state size down to 64 bytes. One megabyte zone can keep about 16 thousand 64-byte states. If the zone storage is exhausted, the server will return the 503 (Service Temporarily Unavailable) error to all further requests.
以上的例子,客户端IP作为了一个键值。注意到这里使用了$binary_remote_addr,而不是$remote_addr。$remote_addr变量的大小在7到15个字节之间,存储占用32位平台上的32或64字节的内存,并且在64位平台上总是有64个字节内存。$binary_remote_addr变量总是占用4个字节内存,存储占用32位平台上的32字节的内存,并且在64位平台上总是有64个字节内存。一个存储区可以保存3200个32字节或1600个64字节。如果存储区满,服务器将对其他请求相应503状态码。


The rate is specified in requests per second (r/s). If a rate of less than one request per second is desired, it is specified in request per minute (r/m). For example, half-request per second is 30r/m.
速率是指每秒的请求指定速率,如果要设置每秒请求低于一个请求的速率,可以设置为r/每分钟,例如30r/m




已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页