加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

利用nginx+lua+redis实现反向代理方法教程

发布时间:2020-12-15 00:57:41 所属栏目:C语言 来源:网络整理
导读:前言 最近因为工作需要,要进行IVR的重构, 我们现在系统接了三家IVR服务商, N个业务, 由于IVR这玩意一般只能外网回调, 而开发环境又不允许外网随便访问, 着实烦人。 所有我们打算重构一把, 封装多家IVR, 对业务透明, 同时回调可以针对多家IVR服务商

前言

最近因为工作需要,要进行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_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服务商, 进行解析callid,然后拼成一个key, 去redis中查询整个key当时写入的value(开发者域名),

最后设置backend整个参数, 然后由nginx进行proxy_pass就完了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

您可能感兴趣的文章:

  • 简介Lua脚本与Redis数据库的结合使用
  • Nginx+Lua+Redis构建高并发Web应用
  • 详解利用redis + lua解决抢红包高并发的问题
  • Go语言中通过Lua脚本操作Redis的方法
  • 利用Lua定制Redis命令的方法详解

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读