设为首页 收藏本站
查看: 1613|回复: 0

[经验分享] OpenResty(nginx)操作redis的初步应用

[复制链接]

尚未签到

发表于 2015-7-22 12:21:26 | 显示全部楼层 |阅读模式
  要想在nginx里访问redis,需要HttpRedis模块 或 HttpRedis2Module模块 或 HttpLuaModule模块的lua-resty-redis库。HttpRedis模块提供的指令少,功能单一,适合做简单缓存,可能以后会扩展。HttpRedis2Module模块比前面的HttpRedis模块操作更灵活,功能更强大。最后一个提供了一个操作redis的接口,可根据自己的情况作一些逻辑处理,类似php开发中的各种扩展,需要自己开发逻辑。如果在安装OpenResty时没有显示的禁用前两个模块,默认是启用的,对于第三个模块可以使用--with-luajit,开启luajit支持,lua-resty-redis库默认是启用的。
  方案一、使用HttpRedis  1、配置nginx.conf



worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
default_type text/html;
set $redis_key $uri;
redis_pass 127.0.0.1:6379;
error_page 404 = @fetch;
}
location @fetch {
root html;
}
}
}
  2、测试配置并重启nginx 3、测试



[iyunv@vm5 conf]# curl -i localhost/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:14:57 GMT
Content-Type: application/x-javascript
Content-Length: 23
Last-Modified: Wed, 20 Feb 2013 19:08:50 GMT
Connection: keep-alive
Accept-Ranges: bytes
// this a test js file
  说明:现在redis缓存里是空的,什么都没存,所以404了,转向命名location @fetch,从本地文件系统提取/common.js文件,本地是存在这个文件的,所以返回了文件内容“// this a test js file”,同时返回http状态码200。 下面我们在redis里存入这个key:



[iyunv@vm5 conf]# redis-cli
redis 127.0.0.1:6379> set '/common.js' '// fetch from redis'
OK
redis 127.0.0.1:6379> keys *
1) "/common.js"
  再次请求



[iyunv@vm5 conf]# curl -i localhost/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:23:13 GMT
Content-Type: application/x-javascript
Content-Length: 19
Connection: keep-alive
// fetch from redis
  ok,结果如我们预期
  方案二、使用HttpRedis2Module
  1、配置nginx.conf



worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location /get {
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass 127.0.0.1:6379;
}
location /set {
set_unescape_uri $key $arg_key;
set_unescape_uri $val $arg_val;
redis2_query set $key $val;
redis2_pass 127.0.0.1:6379;
}
}
}
  2、测试配置并重启nginx 3、测试



[iyunv@vm5 conf]# curl -i localhost/get?key=/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:49:57 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
$19
// fetch from redis
[iyunv@vm5 conf]# curl -i 'localhost/set?key=/common.js&val=set by nginx'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:50:41 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
+OK
[iyunv@vm5 conf]# curl -i localhost/get?key=/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:50:45 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
$12
set by nginx
  ok,结果出来了,其中的$19和$12是返回的数据长度。
  方案三、使用HttpLuaModule模块的lua-resty-redis库
  1、配置nginx.conf



worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
content_by_lua_file conf/lua/redis.lua;
}
}
}
  其中conf/lua/redis.lua代码如下



local cmd = tostring(ngx.var.arg_cmd)
local key = tostring(ngx.var.arg_key)
local val = tostring(ngx.var.arg_val)
local commands = {
get="get",
set="set"
}
cmd = commands[cmd]
if not cmd then
ngx.say("command not found!")
ngx.exit(400)
end
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(1000) -- 1 second
local ok,err = red:connect("127.0.0.1",6379)
if not ok then
ngx.say("failed to connect: ",err)
return
end
if cmd == "get" then
if not key then ngx.exit(400) end
local res,err = red:get(key)
if not res then
ngx.say("failed to get ",key,": ",err)
return
end
ngx.say(res)
end
if cmd == "set" then
if not (key and val) then ngx.exit(400) end
local ok,err = red:set(key,val)
if not ok then
ngx.say("failed to set ",key,": ",err)
return
end
ngx.say(ok)
end
  2、测试配置并重启nginx
  3、测试



[iyunv@vm5 conf]# curl -i 'localhost/?cmd=set&key=/common.js&val=set by lua_resty_redis'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 20:20:07 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
OK
[iyunv@vm5 conf]# curl -i 'localhost/?cmd=get&key=/common.js'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 20:20:15 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
set by lua_resty_redis

ok,结果也是我们所预期的,大功告成!

最后这个方案比较灵活,要想在线上使用,需要编写处理逻辑!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-89467-1-1.html 上篇帖子: redis的基本使用 下篇帖子: 磁盘满时,redis客户端频抛出ConnectionException异常
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表