1 -- access_by_lua_file '/usr/local/lua_test/my_access_limit.lua';
2 ngx.req.read_body()
3
4 local redis = require "resty.redis"
5 local red = redis.new()
6 red.connect(red, '127.0.0.1', '6379')
7
8 local myIP = ngx.req.get_headers()["X-Real-IP"]
9 if myIP == nil then
10 myIP = ngx.req.get_headers()["x_forwarded_for"]
11 end
12 if myIP == nil then
13 myIP = ngx.var.remote_addr
14 end
15
16 if ngx.re.match(ngx.var.uri,"^(/myapi/).*$") then
17 local method = ngx.var.request_method
18 if method == 'POST' then
19 local args = ngx.req.get_post_args()
20
21 local hasIP = red:sismember('black.ip',myIP)
22 local hasIMSI = red:sismember('black.imsi',args.imsi)
23 local hasTEL = red:sismember('black.tel',args.tel)
24 if hasIP==1 or hasIMSI==1 or hasTEL==1 then
25 --ngx.say("This is 'Black List' request")
26 ngx.exit(ngx.HTTP_FORBIDDEN)
27 end
28 else
29 --ngx.say("This is 'GET' request")
30 ngx.exit(ngx.HTTP_FORBIDDEN)
31 end
32 end