OpenResty(nginx)操作redis的初步应用_51CTO博客_openresty redis


本站和网页 https://blog.51cto.com/tianshili/1745331 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

OpenResty(nginx)操作redis的初步应用_51CTO博客_openresty redis
OpenResty(nginx)操作redis的初步应用
关注
ihanxiao2100
OpenResty(nginx)操作redis的初步应用
精选 转载
ihanxiao2100
2016-02-26 15:49:46
博主文章分类:Nginx
文章标签
OpenResty(nginx)操作re
文章分类
Nginx
服务器
http://www.it165.net/database/html/201302/3543.html 要想在nginx里访问redis,需要HttpRedis模块 或 HttpRedis2Module模块 或 HttpLuaModule模块的lua-resty-redis库。HttpRedis模块提供的指令少,功能单一,适合做简单缓存,可能以后会扩展。HttpRedis2Module模块比前面的HttpRedis模块操作更灵活,功能更强大。最后一个提供了一个操作redis的接口,可根据自己的情况作一些逻辑处理,类似php开发中的各种扩展,需要自己开发逻辑。如果在安装OpenResty时没有显示的禁用前两个模块,默认是启用的,对于第三个模块可以使用--with-luajit,开启luajit支持,lua-resty-redis库默认是启用的。方案一、使用HttpRedis1、配置nginx.confworker_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、测试配置并重启nginx3、测试[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
2.redis 127.0.0.1:6379> set '/common.js' '// fetch from redis'
3.OK
4.redis 127.0.0.1:6379> keys *
5.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 redisok,结果如我们预期方案二、使用HttpRedis2Module1、配置nginx.confworker_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、测试配置并重启nginx3、测试[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 nginxok,结果出来了,其中的$19和$12是返回的数据长度。方案三、使用HttpLuaModule模块的lua-resty-redis库1、配置nginx.confwww.it165.networker_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)
end2、测试配置并重启nginx3、测试[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_redisok,结果也是我们所预期的,大功告成!最后这个方案比较灵活,要想在线上使用,需要编写处理逻辑!
打赏
收藏
评论
分享
举报
上一篇:在农村自建房真的不如在城里买房好吗?仔细想想,不禁吃惊
下一篇:让浏览器变身代码编辑器
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
全部评论
()
最热
最新
相关文章
非openresty方式安装Nginx + Lua + Redis 环境
非openresty方式安装Nginx + Lua + Redis 环境
nginx
lua
redis
nginx Ingress openresty
demo1 基于Nginx或是OpenResty的Ingress实现使用 了类似下方的多域名配置, 通过不同的server_name选择合适的后端服务. [root@centos7 work]# cat conf/nginx.conf worker_processes 1; events { wor ...
nginx
html
centos
apache
lua
使用OpenResty、Redis、Lua动态改变Nginx转发地址
前言 项目需要在登录之前调用登陆调度接口来获取登录接口的IP地址,如此便需要Nginx在登陆调度接口之后动态的对登录接口的IP地址进行赋值,因此决定在服务器安装OpenResty,利用Redis缓存功能、Lua的代码编写功能对Nginx的配置文件进行修改。 下载 从下载页 Download下载最新的 ...
Lua
Nginx
Openresty
CentOS
Redis
Nginx使用OpenResty+Lua+Redis进行token鉴权
背景介绍公司有很多项目通过sphinx生成了Html文档,通过nginx转发,前进前端界面展示,方便员工查阅和使用。出于安全考虑,现需要添加一个登陆界面,接入公司的统一认证系统,进行账号校验制定方案通过Oauth2连接公司统一认证系统(springOauth2)前端调用Oauth2获取TokenNginx通过token校验实现鉴权功能此处主要讲Nginx使用Lua脚本连接Redis校验Token是
redis
lua
nginx
openresty nginx+lua
处理参数接口 location /getNameByGender { default_type ''; add_header Content-Type 'text/html; charset=utf-8'; # default_type 'text/html'; # charset utf-8; s ...
mysql
redis
json
sql
lua
OpenResty+mysql+redis
1、 ad_load.lua nginx.conf 1、读取mysql并缓存red
html
redis
mysql
OpenResty部署nginx及nginx+lua
因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用Open
lua
nginx
json
Nginx+Lua+OpenResty安装
Nginx+Lua+OpenResty安装1,安装依赖yum install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl2,下载ngx_openresty-xxx.tar.gz并解压(ngx_openresty-xxx/bundle目录里存放着nginx核心和很多第三方模块,比如有我们需要的Lua和LuaJIT。)wge
Nginx
Lua
OpenResty
openresty自学中之Nginx
为什么选择 Nginx?因为它具有以下特点: 1、处理响应请求很快 在正常的情况下,单次请求会得到更快的响应。在高峰期,Nginx 可以比其它的 Web 服务器更快的响应请求。 2、高并发连接 在互联网快速发展,互联网用户数量不断增加的今天,一些大公司、网站都需要面对高并发请求,如果有一个能够在峰值顶住 10 万以上并发请求的 Server,肯定会得到大家的青睐。理论上,Nginx 支持的
Nginx
nginx实战(一) OpenResty 安装
前言系统环境是Centos7,nginx选用的是openresty版本编译OpenResty下载wgethttps://openresty.org/download/openresty-1.13.6.2.tar.gztarzxmfopenresty-1.13.6.2.tar.gz编译cdopenresty-1.13.6.2/yum-yinstallgccgcc-c++pcrepcre-develz
nginx
openresty
lua
openresty获取nginx 请求方法
ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "
")
其他
Openresty =>nginx+lua
1、下载windows版本的openresty: http://openresty.org/cn/download.html 2、解压 启动nginx.exe: 双击nginx.exe运行 4、验证: 方式一、打开控制台执行命令: tasklist /fi "imagename eq nginx.e
windows
openresty开发系列37--nginx-lua-redis实现访问频率控制
openresty开发系列37--nginx-lua-redis实现访问频率控制一)需求背景在高并发场景下为了防止某个访问ip访问的频率过高,有时候会需要控制用户的访问频次在openresty中,可以找到:set_by_lua,rewrite_by_lua,access_by_lua,content
redis
lua
nginx
h5
连接池
openresty开发系列27--openresty中封装redis操作
openresty开发系列27--openresty中封装redis操作在关于web+lua+openresty开发中,项目中会大量操作redis,重复创建连接-->数据操作-->关闭连接(或放到连接池)这个完整的链路调用完毕,甚至还要考虑不同的 return 情况做不同处理,就很快发现代码中有大量
redis
连接池
lua
封装
sed
openresty开发系列26--openresty中使用redis模块
openresty开发系列26--openresty中使用redis模块在一些高并发的场景中,我们常常会用到缓存技术,现在我们常用的分布式缓存redis是最知名的,操作redis,我们需要引入redis模块 require "resty.redis";我们现在做个可以操作redis进行赋值,读值的案
redis
连接池
配置文件
复用
sed
多级缓存-nginx(OpenResty)本地缓存
在查询商品时,优先查询OpenResty的本地缓存,需求:1.修改item.lua中的read_data函数,优先查询本地缓存,未命中时再查询Redis、Tomcat2.查询Redis或Tomcat成功后,将数据写入本地缓存,并设置有效期3.商品基本信息,有效期30分钟4.库存信息,有效期1分钟 1 ...
nginx 本地缓存
openresty 本地缓存
nginx openresty
redis
lua
OpenResty(Nginx+Lua)开发入门
Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动、关闭、重启 http://www.cnblogs.com/derekchen/archive/2011/02/17/1957209.html agentzh 的 Nginx 教程 http
lua
nginx
java代码
html
配置文件
Memory Fragmentation in OpenResty and Nginx's Shar
Memory fragmentation is a common problem in computer systems though many clever algorithms have emerged to tackle it. Memory fragmentation wastes free memory blocks scattered in a memory region and th
java
How OpenResty and Nginx Allocate and Manage Memory
Our OpenResty® open source web platform is known for its high execution speed and also small memory footprint. We have users running complex OpenResty applications inside embedded system devices like
java
ihanxiao2100
关注
私信
分类列表
更多
# Windows18篇
# Linux59篇
# Nginx23篇
# 工具8篇
# 博客集1篇
近期文章
1.SpringBoot2.x系列教程20--Web开发06之注解方式实现SSM整合
2.Day19_05_Vue教程之第一个Vue应用程序
3.Apache Dubbo 官方正式发布 Spring 6 & Spring Boot 3 支持
4.SpringBoot2.x系列教程60--SpringBoot整合消息队列之ActiveMQ环境配置
5.肯天脱模剂 | 螺杆清洗料助力“包装行业”加工
签到领勋章
返回顶部
举报文章
请选择举报类型
内容侵权
涉嫌营销
内容抄袭
违法信息
其他
具体原因
包含不真实信息
涉及个人隐私
原文链接(必填)
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M
取消
确认
已经收到您得举报信息,我们会尽快审核
打赏
收藏
评论
分享
51CTO首页
内容精选
博客
学堂
精培
企业培训
CTO训练营
开源基础软件社区
LeaTech全球CTO领导力峰会
移动端
公众号矩阵
博客
免费课程
课程排行
直播课
软考学堂
精品班
厂商认证
IT技术
2022年软考
PMP项目管理
在线学习
企业服务
CTO训练营
技术经理研习营
LeaTech峰会
文章
资源
问答
开源课堂
专栏
直播
51CTO博客
首页
关注
排行榜
订阅专栏
学堂
精培
开源社区
CTO训练营
51CTO
班级博客
登录注册
手机随时阅读
写文章
搜索历史
清空
热门搜索
查看【
】的结果
Copyright 2005-2022 51CTO.COM
版权所有 京ICP证060544号
关于我们
官方博客
意见反馈
了解我们
全部文章
在线客服
网站地图
热门标签
友情链接
开源基础软件社区
51CTO学堂
51CTO
汽车开发者社区