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

首頁 > 網站 > Nginx > 正文

利用nginx+lua+redis實現反向代理方法教程

2024-08-30 12:23:15
字體:
來源:轉載
供稿:網友
這篇文章主要給大家介紹了利用nginx+lua+redis實現反向代理方法教程,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

最近因為工作需要,要進行IVR的重構, 我們現在系統接了三家IVR服務商, N個業務, 由于IVR這玩意一般只能外網回調, 而開發環境又不允許外網隨便訪問,

著實煩人。 所有我們打算重構一把, 封裝多家IVR, 對業務透明, 同時回調可以針對多家IVR服務商的不同callid直接轉發到當時請求的同學的

開發域名去。

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

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

環境部署

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

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 是開發環境的配置, 不緩存lua代碼, 修改完lua直接生效, 不然每次要重啟nginx, 上生產環境要關掉, 嚴重影響性能。

不過我們這個需求主要是針對開發環境, 所以無所謂。

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服務商, 進行解析callid, 然后拼成一個key, 去redis中查詢整個key當時寫入的value(開發者域名),

最后設置backend整個參數, 然后由nginx進行proxy_pass就完了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安市| 兰考县| 喜德县| 扬州市| 秦皇岛市| 古浪县| 黔西县| 普陀区| 隆化县| 沈丘县| 宁海县| 高密市| 天气| 宜君县| 荆门市| 通山县| 呼图壁县| 涟水县| 迁西县| 房产| 衡山县| 大埔区| 广水市| 新和县| 阳信县| 苏尼特右旗| 和政县| 澄迈县| 油尖旺区| 龙胜| 淅川县| 卢氏县| 平舆县| 新邵县| 海南省| 河间市| 南郑县| 兴山县| 德化县| 湘乡市| 西城区|