access_by_lua_block { local lim = require"resty.limit.req".new("ip_req_store", 100, 50) -- 差異化限速:VIP用戶放寬限制 local rate = is_vip(ip) and200or100 lim:set_rate(rate) if lim:incoming(ip) == "rejected"then ngx.header["Retry-After"] = 5 return ngx.exit(429) end }
2. 行為分析引擎:識別惡意模式
functiondetect_abnormal(ip) local dict = ngx.shared.ip_behavior local key = ip..":pattern" -- 統(tǒng)計10秒內請求路徑熵值 local entropy = calculate_path_entropy(ip) -- 熵值異常升高判定為掃描行為 if entropy > THRESHOLD then dict:set(key, "scanner", 600) returntrue end end
3. 分布式封禁系統(tǒng)(Redis版)
local redis = require"resty.redis" local red = redis:new() -- 自動拉黑高頻攻擊IP if req_count(ip) > 1000then red:sadd("global:blacklist", ip) red:expire(ip, 3600) end -- 全局黑名單校驗 if red:sismember("global:blacklist", ip) then ngx.exit(403) end