安装openresty+redis+lua
2015-12-29 21:27
826 查看
1.安装luajit: http://luajit.org/download.html
make & make install
2.安装openresty: http://openresty.org/
注意安装条件:apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
3.安装redis: sudo apt-get redis-server
4.安装lua-redis开发包: git clone https://github.com/agentzh/lua-resty-redis.git
加在 http 段里加上: lua_package_path "/your path/?.lua;;";
在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则修改lua后需要重启 Nginx才能生效: lua_code_cache off;
在 Nginx 配置文件中,加入一个Location:location /lua {content_by_lua_file /your path/test.lua;}
5.编写脚本文件test.lua
local redis = require "resty.redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
ngx.say("failed to connect:", err)
return
end
res, err = cache:set("dog", "an aniaml")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", res)
local res, err = cache:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
6.使用一下方法可以根据后缀调用不同的lua文件
location ~ ^/([-_a-zA-Z0-9/]+) {
content_by_lua_file /opt/openresty/p12/lua/$1.lua;
}
7.如果浏览器访问出现下载界面,则MIMEtype不正确,在修改default_typ
在location中加入: default_type text/plain;
make & make install
2.安装openresty: http://openresty.org/
注意安装条件:apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
3.安装redis: sudo apt-get redis-server
4.安装lua-redis开发包: git clone https://github.com/agentzh/lua-resty-redis.git
加在 http 段里加上: lua_package_path "/your path/?.lua;;";
在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则修改lua后需要重启 Nginx才能生效: lua_code_cache off;
在 Nginx 配置文件中,加入一个Location:location /lua {content_by_lua_file /your path/test.lua;}
5.编写脚本文件test.lua
local redis = require "resty.redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
ngx.say("failed to connect:", err)
return
end
res, err = cache:set("dog", "an aniaml")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", res)
local res, err = cache:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
6.使用一下方法可以根据后缀调用不同的lua文件
location ~ ^/([-_a-zA-Z0-9/]+) {
content_by_lua_file /opt/openresty/p12/lua/$1.lua;
}
7.如果浏览器访问出现下载界面,则MIMEtype不正确,在修改default_typ
在location中加入: default_type text/plain;
相关文章推荐
- Redis在linux上的安装
- CentOS7.2 安装redis 3.0.6集群
- 关于spring session redis共享session的一个坑
- 利用redis写webshell
- Spring boot配合Spring session(redis)遇到的错误
- redis数据类型-散列类型
- Redis学习资料汇总
- Codis 是一个分布式 Redis 解决方案
- redis——持久化篇
- redis replica时触发了BGSAVE FAIL的问题
- Redis 启动与授权
- tomcat+redis实现session共享
- php redis 的一些操作常例!
- redis 队列 生产者 消费者模式
- PHP使用Redis二
- 超强、超详细Redis数据库入门教程
- redis 下载启动,设置、查询超时时间
- RedisHelper帮助类
- 用Redis构建分布式锁
- redis使用规范