利用nginx+lua+redis實(shí)現(xiàn)反向代理方法教程
前言
最近因?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)隨便訪問,
著實(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這些常用庫都已經(jīng)打包完畢, 也可以自己安裝, 就是太麻煩。
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 '';
rewrite_by_lua_file /home/work/tengine-2.1.0/conf/lua/ivr.lua;
proxy_pass http://$backend;
}
}
不加resolver的話可能會(huì)報(bào)錯(cuò), 無法解析,加一個(gè)8.8.8.8就可以搞定了。
lua_code_cache 是開發(fā)環(huán)境的配置, 不緩存lua代碼, 修改完lua直接生效, 不然每次要重啟nginx, 上生產(chǎn)環(huán)境要關(guān)掉, 嚴(yán)重影響性能。
不過我們這個(gè)需求主要是針對(duì)開發(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
沒啥特別的, 針對(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就完了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
nginx中的兩個(gè)模塊的proxy_pass的區(qū)別解析
在nginx中配置proxy_pass代理轉(zhuǎn)發(fā)時(shí),如果在proxy_pass后面的url加/,表示絕對(duì)根路徑;如果沒有/,表示相對(duì)路徑,把匹配的路徑部分也給代理走。本文給大家介紹nginx中的兩個(gè)模塊的proxy_pass的區(qū)別,感興趣的朋友一起看看吧2021-11-11
nginx部署多個(gè)前端項(xiàng)目詳細(xì)步驟
最近一臺(tái)服務(wù)器要配置多個(gè)前端項(xiàng)目,當(dāng)然前后端分離就需要nginx來配置了,下面這篇文章主要給大家介紹了關(guān)于nginx部署多個(gè)前端項(xiàng)目的詳細(xì)步驟,需要的朋友可以參考下2023-10-10
利用Nginx實(shí)現(xiàn)URL重定向的簡單方法
使用Nginx的重定向功能時(shí),除了可以重定向到新域名,還可以將請(qǐng)求重定向到特定的協(xié)議上,下面這篇文章主要給大家介紹了關(guān)于如何利用Nginx實(shí)現(xiàn)URL重定向的簡單方法,需要的朋友可以參考下2022-04-04
由于Nginx配置文件問題導(dǎo)致打不開網(wǎng)站unknown directive的解決
這篇文章主要介紹了由于Nginx配置文件問題導(dǎo)致打不開網(wǎng)站unknown directive,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06

