利用nginx+lua+redis實(shí)現(xiàn)反向代理方法教程

這篇文章主要給大家介紹了利用nginx+lua+redis實(shí)現(xiàn)反向代理方法教程,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。

前言

最近因?yàn)楣ぷ餍枰?,要進(jìn)行IVR的重構(gòu), 我們現(xiàn)在系統(tǒng)接了三家IVR服務(wù)商, N個(gè)業(yè)務(wù), 由于IVR這玩意一般只能外網(wǎng)回調(diào), 而開發(fā)環(huán)境又不允許外網(wǎng)隨便訪問(wèn),

著實(shí)煩人。 所有我們打算重構(gòu)一把, 封裝多家IVR, 對(duì)業(yè)務(wù)透明, 同時(shí)回調(diào)可以針對(duì)多家IVR服務(wù)商的不同callid直接轉(zhuǎn)發(fā)到當(dāng)時(shí)請(qǐng)求的同學(xué)的

開發(fā)域名去。

而不同的IVR服務(wù)商的callid參數(shù)是不同的,有的是在url里面(call_id), 有的則是直接post的json數(shù)據(jù)(callid), 所以太扯了。

直接用lua處理下, 查下redis里面這個(gè)callid當(dāng)時(shí)是哪位同學(xué)發(fā)起的請(qǐng)求(請(qǐng)求IVR的時(shí)候會(huì)寫入redis中), 直接proxy_pass到這位同學(xué)的開發(fā)域名去就ok了。

環(huán)境部署

環(huán)境直接用openresty吧, redis、json這些常用庫(kù)都已經(jīng)打包完畢, 也可以自己安裝, 就是太麻煩。

openresty

nginx配置

新建一個(gè)vhost, 配置如下

server { server_name?ivr.com; access_log /home/work/log/nginx/access.ivr.log; error_log /home/work/log/nginx/error.ivr.log; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_read_timeout 30; proxy_connect_timeout 10; location /ivr/ { lua_code_cache off; resolver 8.8.8.8; set?backend'';rewritebyluafile/home/work/tengine?2.1.0/conf/lua/ivr.lua;proxypasshttp://backend ''; rewrite_by_lua_file /home/work/tengine-2.1.0/conf/lua/ivr.lua; proxy_pass http://backend′′;rewriteb?yl?uaf?ile/home/work/tengine?2.1.0/conf/lua/ivr.lua;proxyp?asshttp://backend; } }

不加resolver的話可能會(huì)報(bào)錯(cuò), 無(wú)法解析,加一個(gè)8.8.8.8就可以搞定了。

lua_code_cache 是開發(fā)環(huán)境的配置, 不緩存lua代碼, 修改完lua直接生效, 不然每次要重啟nginx, 上生產(chǎn)環(huán)境要關(guān)掉, 嚴(yán)重影響性能。

不過(guò)我們這個(gè)需求主要是針對(duì)開發(fā)環(huán)境, 所以無(wú)所謂。

lua代碼

local redis = require "resty.redis"local cjson = require "cjson"local cache = redis.new()cache.connect(cache, ‘127.0.0.1’, ‘6379’)local args = ngx.req.get_uri_args()local uri = ngx.var.request_urilocal callid = nillocal channel = 0if string.find(uri, ‘yuntongxun’) then callid = args[“callid”] channel = 0elseif string.find(uri, ‘yunhu’) then ngx.req.read_body() local body_data = ngx.req.get_body_data() local data =? http://www.iis7.com/b/ssyqdq/?cjson.decode(body_data) callid = data[‘call_id’] channel = 1elseif string.find(uri, ‘huawei’) then callid = args[“vSessionsId”] channel = 2else endif callid == nil then ngx.say(uri) ngx.say(cjson.encode(args)) ngx.say(‘callid is empty’) return ''endlocal key = callid … ‘_channel’ … channellocal res = cache:get(key)if res == ngx.null then ngx.say(“cache get error”) return ''endngx.var.backend = res

沒(méi)啥特別的, 針對(duì)多個(gè)IVR服務(wù)商, 進(jìn)行解析callid, 然后拼成一個(gè)key, 去redis中查詢整個(gè)key當(dāng)時(shí)寫入的value(開發(fā)者域名),

最后設(shè)置backend整個(gè)參數(shù), 然后由nginx進(jìn)行proxy_pass就完了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容