大网数据广告活动
服务器运维问答

Apply nginx rate limits to certain IP addresses, and another rate limit to others

· 52CE 我爱测

In our Nginx config we currently have this:- limit_req_zone $binary_remote_addr zone=two:10m rate=15r/m; limit_req zone=two burst=5 nodelay; Now we want to change this so that this rate limit applies to certain IP addresses, and …

问题原文

原帖链接 · user2099762 · CC BY-SA 3.0

In our Nginx config we currently have this:-

limit_req_zone $binary_remote_addr zone=two:10m rate=15r/m;
limit_req zone=two burst=5 nodelay;

Now we want to change this so that this rate limit applies to certain IP addresses, and then have another rate limit that applies to others that is slightly less restrictive.

geo $limited_net {
    default      0;
    111.222.333.444  1;
}

map $limited_net $addr_to_limit {
    0  "";
    1  $binary_remote_addr;  
}

limit_req_zone  $addr_to_limit  zone=two:10m  rate=15r/m;


geo $less_limited_net {
    default      1;
    111.222.333.444  0;
}

map $less_limited_net $addr_to_limit {
    0  "";
    1  $binary_remote_addr;
}

limit_req_zone  $addr_to_limit  zone=three:10m  rate=25r/m;

So the traffic from the IP 111.222.333.444 will be affected by the rate 1st more restrictive rate limit, and not by the second less restrictive one.

We also use cloudflare and have the cloudflare ip addresses set in the /etc/nginx/cloudflare.conf similar to this https://support.cloudflare.com/hc/en-us/articles/200170706-How-do-I-restore-original-visitor-IP-with-Nginx-

Will this give me what I want?

Or should it be more like this?

geo $limited_net { 
default 0; 
111.222.333.444 1; 
} 

map $limited_net $addr_to_limit { 
0 ""; 
1 $binary_remote_addr; 
} 


limit_req_zone $addr_to_limit zone=two:10m rate=15r/m; 
limit_req_zone $binary_remote_addr; zone=three:10m rate=25r/m;

社区高分解答

原帖链接 · Bertware · CC BY-SA 4.0

Even though this question is old, it might be relevant for people stumbling across this in the future:

You can use multiple limit_req_zone directives to process requests from different sources at a different rate:

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_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.

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html#limit_req

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。