网站首页 > 开源技术 正文
目标
使用在nginx中使用lua访问redis数据库。不需要安装lua环境。
安装依赖环境
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
安装LuaJIT
cd /usr/local/
mkdir LuaJIT
cd /usr/local/LuaJIT
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar –xvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make install
安装完成
如果安装时提示找不到wget命令,可以执行yum install wget
这里需要将lib库加到配置中,否则后边会出现找不到库的错误:
# cat /etc/ld.so.confinclude ld.so.conf.d/*.conf# echo "/usr/local/lib" >> /etc/ld.so.conf# ldconfig
安装nginx
cd /usr/local/
mkdir nginx
下载ngx_devel_kit
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
下载lua-nginx-module
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14rc1.tar.gz
下载nginx
wget http://nginx.org/download/nginx-1.15.5.tar.gz
解压文件
tar -xvf v0.3.0.tar.gz
tar -xvf v0.10.14rc1.tar.gz
tar -xvf nginx-1.15.5.tar.gz
编译nginx
cd nginx-1.12.1
./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.14rc1
安装
make
make install
或者合并两个命令(4表示并行运行任务的数量)
make -j 4 && make install
启动nginx
cd /usr/local/nginx/sbin
./nginx
提示找不到luajit的库,需要配置一下
再次启动nginx,启动成功
测试lua配置
修改nginx配置文件nginx.conf:
添加红色区域的内容:
location /lua {
default_type text/plain;
content_by_lua 'ngx.say("Hello, Lua!") ';
}
重新加载配置文件:./nginx -s reload
访问http://ip/lua
说明基本配置成功了。nginx+lua搞定
redis安装
cd /usr/local
mkdir redis
下载
wget http://download.redis.io/releases/ redis-2.8.17.tar.gz
解压
tar -xvf redis-2.8.17.tar.gz
进入解压后的文件夹
cd redis-2.8.17
安装
make
启动redis(&表示后台运行)
cd src
./redis-server &
启动成功后按ctrl+c退出启动界面
测试
./redis-cli
set foo liq ---OK
get foo ---liq
关闭redis
SHUTDOWN
安装lua-resty-redis
从https://github.com/openresty/lua-resty-redis.git下载lua-resty-redis-master后解压
将lib包安装到lua库
解压:
安装
说明库已经安装到/usr/local/lib/lua目录下了
安装完成之后在/usr/local/lib/lua/resty里面会有redis.lua
测试nginx+lua+redis
修改nginx的配置文件nginx.conf
在http中添加以上内容
添加以下两个location,用来测试对redis的访问:
#测试lua set数据到redis
location /lua/set {
default_type text/plain;
content_by_lua_file conf/lua/setKeyValue.lua;
}
#测试lua get数据从redis
location /lua/get {
default_type text/plain;
content_by_lua_file conf/lua/getKey.lua;
}
然后在nginx的conf目录下创建lua目录,lua目录下创建两个文件setKeyValue.lua和getKey.lua
分别编辑setKeyValue.lua文件和getKey.lua文件:
setKeyValue.lua:
--receive request params
local request_method = ngx.var.request_method
local args = nil
local key = nil
local value = nil
--获取参数的值
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
key = args["key"]
value = args["value"]
--connect redis
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
-- 请注意这里 auth 的调用过程
-- check password
local count
count, err = cache:get_reused_times()
if 0 == count then
ok, err = cache:auth("mide123")
if not ok then
ngx.say("failed to auth: ", err)
return
end
elseif err then
ngx.say("failed to get reused times: ", err)
return
end
local res, err = cache:set(key, value)
if not res then
ngx.say("failed to set "..key..": ", err)
return
end
if res == ngx.null then
ngx.say(key.." not found.")
return
end
ngx.say("set redis value >>> "..key..": ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
getKey.lua:
--receive request params
local request_method = ngx.var.request_method
local args = nil
local key = nil
local value = nil
--获取参数的值
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
key = args["key"]
--connect redis
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
-- 请注意这里 auth 的调用过程
-- check password
local count
count, err = cache:get_reused_times()
if 0 == count then
ok, err = cache:auth("mide123")
if not ok then
ngx.say("failed to auth: ", err)
return
end
elseif err then
ngx.say("failed to get reused times: ", err)
return
end
local res, err = cache:get(key)
if not res then
ngx.say("failed to get "..key..": ", err)
return
end
if res == ngx.null then
ngx.say(key.." not found.")
return
end
ngx.say("get from redis >>> "..key..": ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
重启nginx
访问/lua/set,需要加参数:
打开redis客户端查看结果或者使用redis命令
测试/get/key访问,需加参数:
至此,nginx+lua+redis配置成功,测试成功。可以使用lua脚本实现强大的功能了。
猜你喜欢
- 2024-09-14 unity3d开发教程-初始unity(unity3d游戏开发教程)
- 2024-09-14 lua程序在板块模型中的计算机仿真
- 2024-09-14 杨洋回应片酬和演技质疑:演戏是我一辈子的事情
- 2024-09-14 unity3d开发教程-开发环境搭建(unity3d 开发)
- 2024-09-14 c的包管理和构建工具xmake(c语言包管理器)
- 2024-09-14 Nginx内容缓存(nginx内存缓存)
- 2024-09-14 xmake从入门到精通1:安装和更新(xmake github)
- 2024-09-14 Lua 运算符(ll运算符)
- 2024-09-14 【LUA】只需花费你半天时间(我要花费半天的时间)
- 2024-09-14 在.NET Core 中收集数据的几种方式
欢迎 你 发表评论:
- 11-22百度qq号申请注册(在百度上申请qq账号)
- 11-22dos是什么软件
- 11-22系统u盘启动盘(系统 u盘启动)
- 11-22记事本下载(安卓记事本下载)
- 11-22华为电脑wifi开关在哪(华为电脑wifi连接上不能上网怎么办)
- 11-22管理开机启动项(开机启动项管理win10)
- 11-222025动态壁纸图片(2020动态手机壁纸)
- 11-22电脑的设备管理器在哪里打开
- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)

本文暂时没有评论,来添加一个吧(●'◡'●)