利用nginx+lua+redis實現(xiàn)反向代理方法教程
前言
最近因為工作需要,要進行IVR的重構(gòu), 我們現(xiàn)在系統(tǒng)接了三家IVR服務(wù)商, N個業(yè)務(wù), 由于IVR這玩意一般只能外網(wǎng)回調(diào), 而開發(fā)環(huán)境又不允許外網(wǎng)隨便訪問,
著實煩人。 所有我們打算重構(gòu)一把, 封裝多家IVR, 對業(yè)務(wù)透明, 同時回調(diào)可以針對多家IVR服務(wù)商的不同callid直接轉(zhuǎn)發(fā)到當時請求的同學(xué)的
開發(fā)域名去。
而不同的IVR服務(wù)商的callid參數(shù)是不同的,有的是在url里面(call_id), 有的則是直接post的json數(shù)據(jù)(callid), 所以太扯了。
直接用lua處理下, 查下redis里面這個callid當時是哪位同學(xué)發(fā)起的請求(請求IVR的時候會寫入redis中), 直接proxy_pass到這位同學(xué)的開發(fā)域名去就ok了。
環(huán)境部署
環(huán)境直接用openresty吧, redis、json這些常用庫都已經(jīng)打包完畢, 也可以自己安裝, 就是太麻煩。
nginx配置
新建一個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 ''; rewrite_by_lua_file /home/work/tengine-2.1.0/conf/lua/ivr.lua; proxy_pass http://$backend; } }
不加resolver的話可能會報錯, 無法解析,加一個8.8.8.8就可以搞定了。
lua_code_cache 是開發(fā)環(huán)境的配置, 不緩存lua代碼, 修改完lua直接生效, 不然每次要重啟nginx, 上生產(chǎn)環(huán)境要關(guān)掉, 嚴重影響性能。
不過我們這個需求主要是針對開發(fā)環(huán)境, 所以無所謂。
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_uri local callid = nil local channel = 0 if string.find(uri, 'yuntongxun') then callid = args["callid"] channel = 0 elseif string.find(uri, 'yunhu') then ngx.req.read_body() local body_data = ngx.req.get_body_data() local data = cjson.decode(body_data) callid = data['call_id'] channel = 1 elseif string.find(uri, 'huawei') then callid = args["vSessionsId"] channel = 2 else end if callid == nil then ngx.say(uri) ngx.say(cjson.encode(args)) ngx.say('callid is empty') return '' end local key = callid .. '_channel' .. channel local res = cache:get(key) if res == ngx.null then ngx.say("cache get error") return '' end ngx.var.backend = res
沒啥特別的, 針對多個IVR服務(wù)商, 進行解析callid, 然后拼成一個key, 去redis中查詢整個key當時寫入的value(開發(fā)者域名),
最后設(shè)置backend整個參數(shù), 然后由nginx進行proxy_pass就完了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
nginx中的兩個模塊的proxy_pass的區(qū)別解析
在nginx中配置proxy_pass代理轉(zhuǎn)發(fā)時,如果在proxy_pass后面的url加/,表示絕對根路徑;如果沒有/,表示相對路徑,把匹配的路徑部分也給代理走。本文給大家介紹nginx中的兩個模塊的proxy_pass的區(qū)別,感興趣的朋友一起看看吧2021-11-11由于Nginx配置文件問題導(dǎo)致打不開網(wǎng)站unknown directive的解決
這篇文章主要介紹了由于Nginx配置文件問題導(dǎo)致打不開網(wǎng)站unknown directive,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06