玖叶教程网

前端编程开发入门

nginx中使用lua访问redis数据库

目标

使用在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脚本实现强大的功能了。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言