Nginx使用limit_req_zone对同一IP访问进行限流的方法

2019-10-17 16:28:12王旭

简单说明如下:

## 这里取得原始用户的IP地址
map $http_x_forwarded_for $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9.]+),?.*$$firstAddr;
}

## 针对原始用户 IP 地址做限制
limit_req_zone $clientRealIp zone=one:10m rate=1r/s;

同理,我们可以用limit模块对网络爬虫进行限流。

http模块

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

server模块

location / {

limit_req zone=anti_spider burst=2 nodelay;
if ($http_user_agent ~* "spider|Googlebot") {
set $anti_spider $http_user_agent;
}

}

可以用curl -I -A "Baiduspider" www.remotejob.cn/notice.jsp 测试一下

以上这篇Nginx使用limit_req_zone对同一IP访问进行限流的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易采站长站。