使用大牛 ZhangYichun(http://openresty.org/) 提供的集成包快速安装。
在nginx.conf里面配置对应的访问位置:
location /test_redis {
content_by_lua_file lua/test_redis.lua;
}
[root@localhost conf]# curl http://localhost/test_redis
set result: OK
dog: an aniaml
非常简单,下载 ngx_openresty,该集成包中有:Nginx,Lua或Luajit,ngx_lua,以及一些有用的Nginx第三方模块。
例如:
nginx的第三方模块redis,这个包实质就是一个.lua文件,是个库文件,提供一些访问redis的接口:
将其下载下来:
git clone https://github.com/agentzh/lua-resty-redis.git
拷贝:
该包中,有一个 Lib 目录,将 Lib 目录下的文件和子目录拷贝至上文lua_package_path配置的目录(这里是/data/nginx-1.4.2/)下
再写个简单的lua程序连接redis并获取里面的内容:
例如:写个test_redis.lua放在/data0/nginx-1.4.2/lua/下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
res, err = cache:set("dog", "an aniaml")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", res)
local res, err = cache:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
在nginx.conf里面配置对应的访问位置:
location /test_redis {
content_by_lua_file lua/test_redis.lua;
}
[root@localhost conf]# curl http://localhost/test_redis
set result: OK
dog: an aniaml