-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillUrl.lua
More file actions
49 lines (37 loc) · 1 KB
/
killUrl.lua
File metadata and controls
49 lines (37 loc) · 1 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
local function close(red)
if not red then
return
end
--local ok,err = red:close() --直接关闭
--连接池实现释放连接--
local pool_max_idle_time = 1000 --ms
local pool_size = 100 --连接池大小
local ok,err = red:set_keepalive(pool_max_idle_time,pool_size)
if not ok then
ngx.header['killURL'] = 'redis closed err:'..err
ngx.exit(500)
end
end
local redis = require "resty.redis"
local redisKey = ngx.var.killUrls
local request_uri = ngx.var.curURL..ngx.var.request_uri
--创建实例
local red = redis:new()
red:set_timeout(1000) --ms
local ip = ngx.var.redisHost
local port = ngx.var.redisPort
local ok,err = red:connect(ip,port)
if not ok then
ngx.header['killURL'] = err
close(red)
ngx.exit(500)
end
local list,err = red:eval("return redis.call('lrange',KEYS[1],0,-1)",1,redisKey);
for i,v in ipairs(list) do
if string.find(request_uri,v,1,true) then
ngx.header['killURL'] = 'This URL is forbidden to access'
close(red)
ngx.exit(404)
end
end
close(red)