2秒之内访问次数超过100,加入黑名单。
/*
* ip 频繁访问限制策略
* 2秒之内访问次数超过100,加入黑名单。
* 可能存在并发问题
* @constructor
*/
function IPPolicy () {
this.cache = {};
this.blackIpList=[];
var $this = this;
setInterval (function () {
var nowTime = new Date().getTime();
for(ip in $this.cache){
var item = $this.cache[ip];
var timeDif = nowTime - item.visitTime;
if(timeDif<2000 && item.count>100 ){
$this.blackIpList.push(ip)
delete $this.cache[ip];
}else{
item.count = 0;
}
}
},1000)
}
IPPolicy.prototype.addVisitIp = function (ip) {
if(this.cache[ip]){
this.cache[ip].count = this.cache[ip].count+1;
this.cache[ip].visitTime =new Date().getTime();
}else{
this.cache[ip] ={"ip":ip,"count":1,"visitTime":new Date().getTime()}
}
}
可用redis的过期缓存机制来实现频繁访问的缓存功能。
const Redis = require('ioredis');
// const {getmac)= require(' getmac');
// getmac();//获取mac地址
const cache = new Redis({
port: 6300, // Redis port
host: "192.100.50.256", // Redis host
password: "123"
});
cache.get('eggsy').then(data => {
console.log(15, data)
if (data > 5) {
throw new Error('big than 5');
}
if (!data) {
cache.set('eggsy', 1, 'EX', 60);//设置60秒内访问次数大于5次,报错
} else {
cache.incr('eggsy');
}
}).catch(err => {
console.log(err)
})
nginx中限制IP同一时间段的访问次数
#定义一个名为allips的limit_req_zone用来存储session,大小是10M内存,
#以$binary_remote_addr 为key,限制平均每秒的请求为20个,
#1M能存储16000个状态,rete的值必须为整数,
#如果限制两秒钟一个请求,可以设置成30r/m
limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;
...
server{
...
location {
...
#限制每ip每秒不超过20个请求,漏桶数burst为5
#brust的意思就是,如果第1秒、2,3,4秒请求为19个,
#第5秒的请求为25个是被允许的。
#但是如果你第1秒就25个请求,第2秒超过20的请求返回503错误。
#nodelay,如果不设置该选项,严格使用平均速率限制请求数,
#第1秒25个请求时,5个请求放到第2秒执行,
#设置nodelay,25个请求将在第1秒执行。
limit_req zone=allips burst=5 nodelay;
...
}
...
}
#定义一个名为one的limit_zone,大小10M内存来存储session,
#以$binary_remote_addr 为key
#nginx 1.18以后用limit_conn_zone替换了limit_conn
#且只能放在http作用域
limit_conn_zone one $binary_remote_addr 10m;
...
server{
...
location {
...
limit_conn one 20; #连接数限制
#带宽限制,对单个连接限数,如果一个ip两个连接,就是500x2k
limit_rate 500k;
...
}
...
}
...
nginx白名单设置
对于特定的白名单ip我们可以借助geo指令实现。
geo指令定义了一个白名单<nobr aria-hidden="true" style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; transition: none 0s ease 0s; border: 0px; max-width: none; max-height: none; min-width: 0px; min-height: 0px; vertical-align: 0px; line-height: normal; text-decoration: none; white-space: nowrap !important; font-family: KaTeX_Main, "Times New Roman", serif; overflow-wrap: break-word;">limited变量,默认值为1,如果客户端ip在上面的范围内,</nobr><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><annotation encoding="application/x-tex">limited变量,默认值为1,如果客户端ip在上面的范围内,</annotation></semantics></math>limited变量,默认值为1,如果客户端ip在上面的范围内,limited的值为0
2.使用map指令映射搜索引擎客户端的ip为空串,如果不是搜索引擎就显示本身真是的ip,这样搜索引擎ip就不能存到limit_req_zone内存session中,所以不会限制搜索引擎的ip访问
map $limited $limit {
1 $binary_remote_addr;
0 “”;
}
3.设置limit_req_zone和limit_req
limit_req_zone $limit zone=foo:1m rate=10r/m;
limit_req zone=foo burst=5;@[TOC](https://blog.csdn.net/qq_28380979/article/details/%E8%BF%99%E9%87%8C%E5%86%99%E8%87%AA%E5%AE%9A%E4%B9%89%E7%9B%AE%E5%BD%95%E6%A0%87%E9%A2%98)
网友评论