国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

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

2024-08-30 12:28:52
字體:
供稿:網(wǎng)友

前言

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

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

開發(fā)域名去。

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

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

環(huán)境部署

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

openresty

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)掉, 嚴(yá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_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 = 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

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

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 台前县| 苗栗县| 汽车| 中超| 共和县| 铁岭市| 门头沟区| 文化| 岐山县| 阿拉善右旗| 阿瓦提县| 罗甸县| 清远市| 平谷区| 朝阳市| 吐鲁番市| 同江市| 固始县| 周口市| 仙游县| 齐齐哈尔市| 台湾省| 仲巴县| 黎川县| 富川| 荣成市| 蚌埠市| 融水| 昌平区| 登封市| 昌乐县| 班戈县| 丰县| 辽宁省| 博湖县| 扶余县| 潞西市| 奉贤区| 普宁市| 黔西县| 苗栗市|