[error] 7094#0: *1 lua entry thread aborted: runtime error: /usr/local/nginx/conf/lua/test_redis_basic.lua:11: module 'resty.redis' not found: no field package.preload['resty.redis']
no file '/home/www/lua-redis-parser-0.12/resty/redis.lua'
no file './resty/redis.lua'
no file '/usr/local/share/lua/5.1/resty/redis.lua'
no file '/usr/local/share/lua/5.1/resty/redis/init.lua'
no file '/usr/local/lib/lua/5.1/resty/redis.lua'
no file '/usr/local/lib/lua/5.1/resty/redis/init.lua'
no file '/usr/share/lua/5.1/resty/redis.lua'
no file '/usr/share/lua/5.1/resty/redis/init.lua'
no file './resty/redis.so'
no file '/usr/local/lib/lua/5.1/resty/redis.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/resty/redis.so'
no file '/usr/lib/lua/5.1/resty/redis.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/resty.so'
no file '/usr/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
local function close_redis(redis_instance)if not redis_instance then return
end
local ok,err = redis_instance:close();
if not ok then ngx.say(
"close redis error : ",err);end
end
local redis = require("resty.redis");
--local redis = require "redis"
-- 创建一个redis对象实例。在失败,返回nil和描述错误的字符串的情况下
local redis_instance = redis:new();
--设置后续操作的超时(以毫秒为单位)保护,包括connect方法
redis_instance:set_timeout(1000)
--建立连接
local ip = '127.0.0.1'
local port = 6379
--尝试连接到redis服务器正在侦听的远程主机和端口
local ok,err = redis_instance:connect(ip,port)
if not ok then
ngx.say("connect redis error : ",err)
return close_redis(redis_instance);
end
--Redis身份验证
--local auth,err = redis_instance:auth("");
--if not auth then
-- ngx.say("failed to authenticate : ",err)
--end
--调用API进行处理
local resp,err = redis_instance:set("msg","hello world")
if not resp then
ngx.say("set msg error : ",err)
return close_redis(redis_instance)
end
--调用API获取数据
local resp, err = redis_instance:get("msg")
if not resp then
ngx.say("get msg error : ", err)
return close_redis(redis_instance)
end
--得到的数据为空处理
if resp == ngx.null then
resp = 'this is not redis_data' --比如默认值
end
ngx.say("msg : ", resp)
close_redis(redis_instance)
我们通过curl 在shell脚本中测试以上配置文件
重启Nginx服务器:
root@iZ236j3sofdZ:/usr/local/nginx/conf# service nginx restart* Stopping Nginx Server...* Starting Nginx Server... nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.conf:69
checking for LuaJIT library in /usr/bin/luajit/lib and (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... not found
checking
for LuaJIT library in /usr/bin/luajit/lib and (specified by the LUAJIT_LIB and LUAJIT_INC env) ... not found .
/configure: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as /usr/bin/luajit/lib and LUAJIT_INC (path for lua.h) , but we cannot find LuaJIT there.