OpenResty 这里就不介绍了,可以阅读 OpenResty(nginx)操作mysql的初步应用 或 参阅 http://openresty.org

要想在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、测试
[root@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:
[root@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"

再次请求

[root@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、测试
[root@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
[root@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
[root@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、测试
[root@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
[root@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,结果也是我们所预期的,大功告成!
最后这个方案比较灵活,要想在线上使用,需要编写处理逻辑!

转载于:https://blog.51cto.com/haoyun/1136994

OpenResty(nginx)操作redis的初步应用相关推荐

  1. OpenResty(nginx)操作mysql的初步应用

    OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项. OpenResty 通 ...

  2. openresty 操作 redis

    文章目录 1.redis连接池 2.编写测试脚本 前言: openresty .lua .redis 的 安装这里就不再赘述了,请自行百度. 使用到了OpenResty,很核心的目的是为了解决高并发的 ...

  3. Nginx 连接 Redis 实现数据库操作

    Nginx 连接 Redis 实现数据库操作 实现的功能:当有新的连接到达Nginx时,在redis记录连接数量. 1. redis 创建数据库 安装完成redis之后,通过set connums 0 ...

  4. Openresty通过Lua+Redis 实现动态封禁IP

    需求背景 为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝提供服务.并且可以设置失效 环境准备 linux version:centos ...

  5. Linux安装Nginx、Redis、django

    部署Nginx 部署Redis 安装Redis redis事物 服务器管理命令 慢查询日志 主从复制 Redis-Sentinel cluser分片集群 安装python 操作redis数据 部署Dj ...

  6. 函数指针--Nginx和Redis中两种回调函数写法

    1.Nginx和Redis中两种回调函数写法 #include <stdio.h>//仿Nginx风格 //结构外声明函数指针类型 typedef void (*ngx_connectio ...

  7. nginx lua redis 访问频率限制(转)

    1. 需求分析 Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的 ...

  8. Nginx + Lua + redis (一)(转)

    使用 Lua 脚本语言操作 Redis. 由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua 脚本文件. 要使用 ...

  9. ngx_lua操作Redis和Mysql

    功能简介 通过ngx_lua对redis进行数据的插入和取出 ngx_lua对mysql的增删改查 浏览器以json格式返回数据 将频繁调用的cjson设置全局,一开始就加载 结果显示 redis数据 ...

最新文章

  1. Arch Linux PDF格式文件无法显示中文
  2. 31页官方PPT,回顾史上最大芯片WSE:科技的壮丽美感!
  3. ExtJs自学教程(1):一切从API開始
  4. flask_sqlalchemy连接Mysql报TypeError: create_engine() got an unexpected keyword argument 'encoding'解决办法
  5. 提升win双屏体验_海信双屏A6L评测,在自由阅读中植入护眼水墨屏
  6. 智慧办公的AI博弈——看飞企互联如何接招!
  7. 扯淡!C语言怎么可能被淘汰呢?
  8. 在5分钟内将Spring Boot作为Windows服务启动
  9. BUG_ON()、panic()、dump_stack()几种内核调试手段
  10. linux arm ffmpeg configure文件,ffmpeg库的交叉编译记录
  11. 2016CCPC长春:Sequence II(主席树)
  12. 编译mcu media server
  13. AIDA64内存与缓存测试过了算稳定吗_买了B460主板的你,究竟需要怎样的内存
  14. 将计算机网口虚拟串口软件,虚拟串口及其在串口转以太网中的应用
  15. Macbook reset PRAM
  16. win7 IE中使用svg时利用symbol的方式出现卡死现象
  17. 基于数组判断字符串是否是回文
  18. 微信小程序开发《6 .框架之逻辑层》
  19. 2022年上半年计算机技术与软件专业技术资格(水平)考试陕西考区报名须知
  20. esxi云虚拟服务器如何搭建,如何搭建esxi环境?

热门文章

  1. AndroidStudio安卓原生开发_UI高级_自定义主题和样式---Android原生开发工作笔记129
  2. C++_程序内存模型_内存四区_代码区_全局区_每种区域都存放什么样的变量---C++语言工作笔记028
  3. IntelliJ Idea工作笔记009---代码没有错,但是在IDEA报错的原因
  4. SpringCloud工作笔记066---断路器(Curcuit Breaker)模式
  5. android学习笔记---56_activity切换动画与页面切换动画,自定义activity窗口切换动画效果的实现.
  6. 1004 C语言设置测试数据个数和测试结束标志
  7. mac上解决中文乱码, arara实现LaTex多命令执行, LaTeXiT法文界面转英文
  8. 随想录(windows静态库和动态库)
  9. C语言和设计模式(观察者模式)
  10. C语言和设计模式(代理模式)