单独匹配域名的CC攻击
如下单独配置匹配域名www.kuaiban.com 的denycc函数:
- 匹配域名
if ngx.var.host == "www.kuaiban.com" then
- 配置本地变量
local CCrate="500/120" , 不使用全局配置
- 需要在
config.lua 启用CC防护开关(CCDeny="on"),并将参数配置高一些(比如CCrate="1000/60"),默认还需要匹配
- 如果默认不需要,只匹配准确域名,可以在
init.lua 文件的denycc将默认删除
- 一定要注意缩进与语法格式
| Bash |
|---|
| function denycc()
--www.kuaiban.com config 500/120
if CCDeny then
if ngx.var.host == "www.kuaiban.com" then
local CCrate="500/120"
local uri=ngx.var.uri
CCcount=tonumber(string.match(CCrate,'(.*)/'))
CCseconds=tonumber(string.match(CCrate,'/(.*)'))
local token = getClientIp()..uri
local limit = ngx.shared.limit
local req,_=limit:get(token)
if req then
if req > CCcount then
log('CC', ngx.var.request_uri, "CC Attack blocked: count="..req.." rate="..CCrate, "CCDENY")
ngx.exit(503)
return true
else
limit:incr(token,1)
end
else
limit:set(token,1,CCseconds)
end
end
end
-- 如下是默认全局匹配,如不需要可将删除
if CCDeny then
local uri=ngx.var.uri
CCcount=tonumber(string.match(CCrate,'(.*)/'))
CCseconds=tonumber(string.match(CCrate,'/(.*)'))
local token = getClientIp()..uri
local limit = ngx.shared.limit
local req,_=limit:get(token)
if req then
if req > CCcount then
log('CC', ngx.var.request_uri, "CC Attack blocked: count="..req.." rate="..CCrate, "CCDENY")
ngx.exit(503)
return true
else
limit:incr(token,1)
end
else
limit:set(token,1,CCseconds)
end
end
return false
end
|