之前的csdn找不回来了,决定重新注册一个。望支持~~~
场景:nginx读取redis存储的标识进行重定向或ip拦截
废话不多说,直接lou代码:
location /{
set $tomcat_ip '';
rewrite_by_lua '
local redis = require "resty.redis"
local red = redis::new()
red:set_timeout(1000) -- 1 sec
local ok,err = red:connection("127.0.0.1",6379)
if not ok then
ngx.say("fail to connect: ", err)
return
end
--请注意这里auth的调用过程
local count
count,err = red:get_refused_times()
if 0 == count then
ok,err = red:auth("aaaa")
if not ok then
ngx.say("fail to auth: ",err)
return
end
elseif err then
ngx.say("failed to get reused times: ", err)
return
end
local resp,err = red:get("dog")
if not resp then
ngx.say("get msg error: ", err)
return err
end
if resp == ngx.null then
ngx.say("this is not redis data")
return null
end
ngx.var.tomcat_ip=resp
-- ngx.say("set result: ", ok)
';
#echo $tomcat_ip
proxy_pass $tomcat_ip
}注意: 别忘nginx先加入redis组件~~~