基于请求正文内容的nginx条件代理传递

0lvr5msh  于 2023-05-16  发布在  Nginx
关注(0)|答案(4)|浏览(131)

我试图配置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,
              }
6fe3ivhb

6fe3ivhb1#

我找到解决办法了。
我在nginx(open resty)配置文件中做了以下更改

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

ujv3wf0j2#

我能说的最好的问题是,在执行if语句时,变量$request_body可能没有被读入内存。
建议的替代方案是使用lua支持或compile nginx with the echo modules and run echo_request_body

tquggr8v

tquggr8v3#

一个自动化测试的工作示例,以防对其他人有用:https://github.com/ericminio/learning-nginx/tree/master/payload_filter
配置

map $request_body $matching {
    default       'not_matching';
    include       /etc/nginx/conf.d/matching;
}

server {                                                                              
    default_type  text/plain;

    location /ping {
        return 200 'pong';
    }

    location / {  
        proxy_set_header 'X-Matching' $matching;
        proxy_pass http://localhost/routing;           
    } 

    location /routing {
        try_files /unknown-file-to-trigger-redirect @_$http_x_matching;
    }
    location @_matching {
        proxy_pass http://host.docker.internal:5015;
    }
    location @_not_matching {
        proxy_pass http://host.docker.internal:5066;
    } 

}
lymnna71

lymnna714#

当使用集成时,这并不容易,比如在某些情况下,你的nginx并不允许你做额外的代理来改变整个设计,那么你可以尝试nginx-if-request-body来实现结果。

http {
 ....
     
 map "$uri" $forward_status {
    default 100; # 100 means nothing return, continue to proxy phase
    "~*.+?\.(css|js|bmp|gif|ico|jpeg|jpg|pict|png|svg|swf|tif)$" 418;
 }
 map "$request_body" $forward_status_by_body {
    default 100;
    "abc123xxx" 418;
    "~*.+?\.(css|js|bmp|gif|ico|jpeg|jpg|pict|png|svg|swf|tif)$" 418;
  }

server {
 ...
    error_page 418 =200 @welcome_if_request_body;

    location @welcome_if_request_body {
        add_header Content-Type text/plain;
        return 200 "welcome_if_request_body, you hit it";
    }

    location = / {
        if_request_body on;
        return_status_if_body_eq "ASD" 418 on;
        return_status_if_body_eq "foo" 418;
        return_status_if_body_eq "john" 418;
        return_status_if_body_startswith "report" 418;
        return_status_if_body_contains "report" 418;
        return_status_if_body_regex "^[\d]+?abc" 418;
        return_status_if_variable_map_to $forward_status;
        return_status_if_variable_map_to $forward_status_by_body;
        proxy_pass http://localhost:7777;

    }
...
  }
}

相关问题