nginx使用lua做灰度发布

博客围绕nginx条件代理展开,作者尝试配置nginx,使其仅在$request_body变量匹配特定正则表达式时将请求传递给另一服务器,但未成功。推测问题是if语句执行时变量未读入内存,建议使用lua支持或用echo模块编译nginx并运行echo_request_body。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
   }
   }
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值