# cd /usr/local/src # wget https://github.com/openresty/lua-resty-redis/archive/master.zip # unzip master.zip # cd lua-resty-redis-master/ # make && make install make: Nothing to be donefor `all'. install -d /usr/local/lib/lua//resty install lib/resty/*.lua /usr/local/lib/lua//resty
# vim /usr/local/lua/request.lua local redis = require"resty.redis" local cache = redis:new() -- redis连接 local ok,err = cache.connect(cache, '127.0.0.1', '25612')
ifnot ok then ngx.say("failed to connect redis cache:", err); return end
-- redis认证 local count count, err = cache:get_reused_times() if0 == count then ok,err = cache:auth("xxxxx") ifnot ok then ngx.say("failed to auth redis: " , err) return end elseif err then ngx.say("failed to get reused times: ", err) return end -- 选择使用的redis数据库 cache:select(7) -- 当前访问地址
-- 获取redis 中的值 local res, err = cache:get(url_full_path) -- ngx.req.set_uri(url) if res then --ngx.say(sch .. "://" .. host .. url .. " " .. res) ngx.redirect(res,302) end # nginx -s reload
在redis 写入key 并测试
1 2 3 4 5 6 7
# redis-cli -h 127.0.0.1 -p 25612 127.0.0.1:25612> AUTH xxxxxxxxxx OK 127.0.0.1:25612> SELECT 7 OK 127.0.0.1:25612[7]> set https://www.ssjinyao.com/lua/test https://www.ssjinyao.com/2019/03/16/%E5%85%B3%E4%BA%8E%E6%9F%90%E7%B3%BB%E7%BB%9F%E9%98%B5%E5%8F%91%E6%80%7%E7%BD%91%E7%BB%9C%E5%A4%84%E7%90%86/ OK