nginx使用lua做灰度发布

https://stackoverflow.com/questions/44245660/nginx-conditional-proxy-pass-based-on-request-body-content

nginx条件代理根据请求正文内容传递

我试图配置nginx代理将请求传递给另一个服务器,只有当$ request_body变量匹配特定的正则表达式。但它不适合我。

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

2

最好的我可以告诉你的问题是,在$request_body你的if语句执行时,变量可能没有被读入内存。

建议的替代方案是使用lua支持或使用echo模块编译nginx并运行echo_request_body

1

I found the solution.

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;
   }
   }
         
发布了51 篇原创文章 · 获赞 9 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_39891030/article/details/88748417