Nginx + lua + redis implements reverse proxy configuration

Source: Internet
Author: User
Tags json lua redis require

Background

We need to reconstruct IVR recently. Now we have three IVR service providers and N businesses. Because IVR can only be called back from the Internet, the development environment does not allow access from the Internet,

Really annoying. We intend to rebuild one, encapsulate multiple IVR instances, and make the callback transparent to the business. At the same time, different callids of multiple IVR service providers can be directly forwarded to those of the students who requested it at that time.

Go to the development domain name.

The callid parameters of different IVR service providers are different. Some are in the url (call_id), and some are directly post json data (callid.

Use lua to directly process the call ID in redis and check the request initiated by the caller (the request will be written to redis when requesting IVR ), directly go to the development domain name of this student by using proxy_pass.

Environment deployment

You can directly use openresty in the environment. Common databases such as redis and json have been packaged and can be installed by yourself, which is too troublesome.
Openresty

Nginx configuration

Create a new vhost and configure it as follows:


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;
    }  

 

If resolver is not added, an error may be reported and cannot be parsed. You can simply add 8.8.8.8.

Lua_code_cache is the configuration of the development environment. If you do not cache the lua code, the modified lua takes effect directly. Otherwise, you need to restart nginx each time and turn it off in the production environment, seriously affecting the performance.

However, this requirement is mainly for the development environment, so it doesn't matter.

Lua code


Local redis = require "resty. redis"
Local cjson = require "cjson"
Local cache = redis. new ()
Cache. connect (cache, '192. 0.0.1 ', '123 ')

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

Nothing special. For multiple IVR service providers, resolve the callid and combine it into a key to query the value (developer domain name) written by the entire key in redis ),

Finally, the entire backend parameter is set, and then nginx will complete proxy_pass.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.