HttpLimitReqModule

本文介绍Nginx的HttpLimitReq模块,该模块可通过漏桶算法限制给定会话或特定地址的请求数量。文章详细解释了如何配置limit_req_zone及limit_req指令来实现平滑的请求处理,并举例说明了不同参数设置的效果。
http://wiki.nginx.org/NginxHttpLimitReqModule

(Redirected from NginxHttpLimitReqModule)
Synopsis
This module allows you to limit the number of requests for a given session, or as a special case, with one address.

Restriction done using leaky bucket.

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_log_level info|notice|warn|error

Default: warn

Context: http

Controls the log level of the rejected requests. Delayed requests are logged at the next less severe level, though, for example when limit_req_log_level is set to "error", delayed requests are logged at "warn".


Syntax: limit_req_zone $session_variable zone=name_of_zone:size rate=rate

Default: none

Context: http

The directive describes the area, which stores the state of the sessions. The values of the sessions is determined by the given variable. Example of usage:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
In this case, the session state is allocated 10MB as a zone called "one", and the average speed of queries for this zone is limited to 1 request per second.

The sessions are tracked per-user in this case, but note that instead of the variable $remote_addr, we've used the variable $binary_remote_addr, reducing the size of the state to 64 bytes. A 1 MB zone can hold approximately 16000 states of this size.

The speed is set in requests per second or requests per minute. The rate must be an integer, so if you need to specify less than one request per second, say, one request every two seconds, you would specify it as "30r/m".

Syntax: limit_req zone=zone burst=burst [nodelay]

Default: none

Context: http, server, location

The directive specifies the zone (zone) and the maximum possible bursts of requests (burst). If the rate exceeds the demands outlined in the zone, the request is delayed, so that queries are processed at a given speed. Excess requests are delayed until their number does not exceed a specified number of bursts. In this case the request is completed the code "Service unavailable" (503). By default, the burst is zero.

For example, the directive

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

server {
location /search/ {
limit_req zone=one burst=5;
}
allows a user no more than 1 request per second on average, with bursts of no more than 5 queries. If the excess requests within the limit burst delay are not necessary, you should use the nodelay:

limit_req zone=one burst=5 nodelay;
实际上,Nginx 并没有 `HttpLimitZoneModule` 模块,你可能想问的是 `HttpLimitReqModule` 或 `HttpLimitConnModule`。以下分别介绍这两个模块: ### HttpLimitReqModule #### 介绍 该模块用于限制客户端的请求速率,可以有效防止恶意流量和请求过载问题[^1]。 #### 使用方法 通过 `limit_req_zone` 和 `limit_req` 两个指令来配置限流。示例配置如下: ```nginx limit_req_zone $binary_remote_addr zone=mylimit:10m rate=2r/s; server { location ~* \.(html)$ { limit_req zone=mylimit; } } ``` 上述配置中,`limit_req_zone` 定义了一个名为 `mylimit` 的限流区域,使用 `$binary_remote_addr`(客户端 IP 地址的二进制形式)作为限流的键,分配 10MB 的内存用于存储限流状态,速率限制为每秒 2 个请求。在 `server` 块的 `location` 中使用 `limit_req` 指令引用这个限流区域,对匹配 `.html` 后缀的请求进行限流[^1]。 ### HttpLimitConnModule #### 介绍 该模块用于限制每个客户端 IP 的连接数,可以防止客户端建立过多的连接,从而保障服务器资源的合理使用。 #### 使用方法 通过 `limit_conn_zone` 和 `limit_conn` 两个指令来配置连接数限制。示例配置如下: ```nginx limit_conn_zone $binary_remote_addr zone=perip:10m; server { location /download/ { limit_conn perip 1; } } ``` 上述配置中,`limit_conn_zone` 定义了一个名为 `perip` 的连接限制区域,使用 `$binary_remote_addr` 作为键,分配 10MB 的内存用于存储连接状态。在 `server` 块的 `location` 中使用 `limit_conn` 指令引用这个区域,并限制每个客户端 IP 的连接数为 1。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值