nginx conditional proxy pass based on request body content

Viewed 12217

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

}

request body is::

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

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

That is not so easy when come to using integration , like some circumstances your nginx does not really allow you to do extra proxy stuff to change the whole design, then you can try nginx-if-request-body to achieve the result

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;

    }
...
  }
}
Related