–
安装lua-redis-parser,lua-resty-redis是openresty的一个组件,简单来说,它提供一个lua语言版的redis API,使用socket(lua sock)和redis通信。 [root@master-10 ~]#git clone https://github.com/openresty/lua-resty-redis.git [root@master-10 ~]#mkdir -p /usr/local/nginx/lua [root@master-10 ~]#mv lua-resty-redis /usr/local/nginx/lua [root@master-10 ~]#ll /usr/local/nginx/lua/ total 4 drwxr-xr-x 5 root root 4096 Aug 8 13:39 lua-resty-redis 在http段增加以下配置文件,去找对应的库文件 [root@master-10 ~]#cat /usr/local/nginx/conf/nginx.conf http { lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;"; 本机启动redis服务,监听端口:127.0.0.1:6380 下面是nginx配置 [root@master-10 ~]#cat /usr/local/nginx/conf/conf.d/80.conf server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /lua { echo "Hello Lua"; } location /lua_test { content_by_lua ' ngx.say("Hello Lua! Tinywan") '; } #content_by_lua_block location /content_by_lua_block { default_type 'text/plain'; content_by_lua_block { ngx.say('Hello : content_by_lua_block') } } location /lua_redis_basic { default_type 'text/html'; lua_code_cache off; content_by_lua_file /lua/test_redis_basic.lua; } } 下面是lua文件内容 [root@master-10 ~]#cat /lua/test_redis_basic.lua local function close_redis(redis_instance) if not redis_instance then return end local ok,err = redis_instance:close() -- 1 ok, 0 err 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 = 6380 --尝试连接到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) [root@master-10 ~]#service nginx reload nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/conf.d/80.conf:27 Reloading nginx: [ OK ] 警告:这个alert是因为objstore.conf中把lua_code_cache为off;若设置为off,nginx不缓存lua脚本,每次改变lua代码, 不必reload nginx即可生效;这便于开发和测试。但禁用缓存对性能有影响,故正式环境下一定记得设置为on; [root@master-10 ~]#curl localhost/lua Hello Lua [root@master-10 ~]#curl localhost/lua_test Hello Lua! Tinywan [root@master-10 ~]#curl localhost/content_by_lua_block Hello : content_by_lua_block [root@master-10 ~]#curl localhost/lua_redis_basic msg : hello world redis2-nginx-module和lua-resty-redis redis2-nginx-module是一个openresty自带的模块。它能够把请求转发给upstream(redis2_pass)。注意它和lua-resty-redis不同, lua-resty-redis是一个lua语言版的redis API,使用socket(lua sock)和redis通信。而redis2-nginx-module是把请求转发给别的upstream。
参考https://www.cnblogs.com/tinywan/p/6534151.html
–
–
–
评论前必须登录!
注册