local redis = require"resty.redis" local cache = redis.new() local ok, err = cache.connect(cache, '127.0.0.1', '6379') ngx.req.read_body() local method = ngx.var.request_method
cache:set_timeout(60000)
ifnot ok then -- ngx.say("failed to connect:", err) return end
if method == 'POST'then local data = ngx.req.get_body_data() if data then local i = ngx.re.match(data, "[0-9]{11}") local phone = i[0] local exist = cache:exists(phone) if exist == 0then cache:set(phone, "phone") cache:expire(phone, 86400) else ngx.redirect("http://www.google.com", 302) ngx.say(phone, ' is in baned') return ngx.exit(403) -- ngx.say(phone, ' is in baned') -- return ngx.exit(403) end end end
local ok, err = cache:close()
ifnot ok then -- ngx.say("failed to close:", err) return end