一、代码
1.1 准备
1. 启动redis
src/redis-server
2. 启动redis客户端,设置domain
src/redis-cli
set domain *.baidu.com
1.2 代码
local redis = require "resty.redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
ngx.say("failed to connect:", err)
return
end
local res, err = cache:get("domain")
if not res then
ngx.say("failed to get domain: ", err)
return
end
if res == ngx.null then
ngx.say("domain not found.")
return
end
ngx.say("domain: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
1.3 Nginx配置文件
location /lua {
content_by_lua_file conf/lua/hello.lua;
}
二、运行结果
现在就可以访问那个Nginx机器了
curl 192.168.175.131/lua
参考资料:
Nginx + Lua + Redis:http://blog.csdn.net/vboy1010/article/details/7892120
Lua:Nginx Lua环境配置,第一个Nginx Lua代码:http://blog.csdn.net/guowenyan001/article/details/48250589
Redis下载、安装、简单实用:http://blog.csdn.net/guowenyan001/article/details/49909233