基于请求正文内容的nginx条件代理传递 [英] nginx conditional proxy pass based on request body content

查看:70
本文介绍了基于请求正文内容的nginx条件代理传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在$ request_body变量与特定正则表达式匹配时,我才尝试配置nginx代理将请求传递给另一台服务器.但这对我不起作用.

I am trying to configure nginx to proxy pass the request to another server, only if the $request_body variable matches on a specific regular expression.But it is not working for me.

 server{
        listen 80 default;
        server_name www.applozic.com;

        location / {
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $remote_addr;
                 proxy_set_header Host $http_host;

                if ($request_body ~* (.*)appId(.*)) {
                   proxy_pass http://apps.applozic.com;
                }
        }

}

请求正文是::

              {
               "applicationId": "appId",
               "authenticationTypeId": 1,
               "enableEncryption": false,
               "notificationMode": 0,
               "deviceType": 4,
              }

推荐答案

我找到了解决方案.

我在nginx(打开Resty)配置文件中做了以下更改

I did following changes in nginx(open resty) config file

upstream algoapp {
   server 127.0.0.0.1:543;
}
upstream main {
   server 127.0.0.1:443;
}

location /rest/ws/login {
   proxy_set_header X-Forwarded-Host $host;
   proxy_set_header X-Forwarded-Server $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Url-Scheme $scheme;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header Host $http_host;
   proxy_redirect off;
   if ($request_method = OPTIONS ) {
   proxy_pass https://127.0.0.1:543;
   }
   if ($request_method = POST ) {
   set $upstream '';
   access_by_lua '
   ngx.req.read_body()
   local data = ngx.req.get_body_data()
   local  match = ngx.re.match(ngx.var.request_body, "appId")
   if match then
      ngx.var.upstream = "algoapp"
   else
   ngx.var.upstream = "main"
   end
   ';
   proxy_pass https://$upstream;
   }
}

这篇关于基于请求正文内容的nginx条件代理传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆