1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
-- 本次请求的响应码,如果不是 200/304 或特定响应码,视为异常
local status_code = ngx.var.status
local res_status = 0
if ( status_code == '200' ) or ( status_code == '304' ) then
res_status = 1
elseif ( status_code == '999' ) then
res_status = 1
end
if ( res_status == 0 ) then
local ip = ngx.var.remote_addr -- 获取请求使用的ip地址
local dict = ngx.shared.acl_ip_tables
num = dict:get(ip)
if num == nil then
-- 第一次异常访问
ngx.log(ngx.ERR, "num: ",num, " status_code:", status_code," remote add: ", ngx.var.remote_addr)
dict:set(ip, 1, 180) -- 初始化计数
else
-- 存在异常时仍继续访问 +1
num = dict:incr(ip, 1)
ngx.log(ngx.ERR, "acl_num+1=", num)
-- 3分钟内异常大于9次,延长至10分钟
if ( num == 9 ) then
dict:set(ip, 9, 600)
ngx.log(ngx.ERR, "num: ",num, " set 10min: ", ip)
end
end
end
|