nginx中使用lua訪問redis資料庫

nginx中使用lua訪問redis數據庫

目標

使用在nginx中使用lua訪問redis數據庫。不需要安裝lua環境。

安裝依賴環境

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

nginx中使用lua訪問redis數據庫

安裝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庫加到配置中,否則後邊會出現找不到庫的錯誤:

nginx中使用lua訪問redis數據庫

# cat /etc/ld.so.confinclude ld.so.conf.d/*.conf# echo "/usr/local/lib" >> /etc/ld.so.conf# ldconfig

nginx中使用lua訪問redis數據庫

安裝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

nginx中使用lua訪問redis數據庫

提示找不到luajit的庫,需要配置一下

nginx中使用lua訪問redis數據庫

再次啟動nginx,啟動成功

測試lua配置

修改nginx配置文件nginx.conf:

nginx中使用lua訪問redis數據庫

添加紅色區域的內容:

location /lua {

default_type text/plain;

content_by_lua 'ngx.say("Hello, Lua!") ';

}

重新加載配置文件:./nginx -s reload

nginx中使用lua訪問redis數據庫

訪問http://ip/lua

nginx中使用lua訪問redis數據庫

說明基本配置成功了。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庫

nginx中使用lua訪問redis數據庫

解壓:

nginx中使用lua訪問redis數據庫

nginx中使用lua訪問redis數據庫

安裝

nginx中使用lua訪問redis數據庫

說明庫已經安裝到/usr/local/lib/lua目錄下了

安裝完成之後在/usr/local/lib/lua/resty裡面會有redis.lua

nginx中使用lua訪問redis數據庫

測試nginx+lua+redis

修改nginx的配置文件nginx.conf

nginx中使用lua訪問redis數據庫

在http中添加以上內容

添加以下兩個location,用來測試對redis的訪問:

nginx中使用lua訪問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

nginx中使用lua訪問redis數據庫

分別編輯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,需要加參數:

nginx中使用lua訪問redis數據庫

打開redis客戶端查看結果或者使用redis命令

nginx中使用lua訪問redis數據庫

測試/get/key訪問,需加參數:

nginx中使用lua訪問redis數據庫

至此,nginx+lua+redis配置成功,測試成功。可以使用lua腳本實現強大的功能了。


分享到:


相關文章: