How to route traffic (reverse Proxy) with HAProxy based on request body
Asked Answered
A

2

7

I am attempting to route the following request to the appropriate servers based on the URL identified in the POST body below. I am hoping to accomplish this via a reverse proxy using HAProxy.

E.g. I would like to direct all requests to HAProxy, than have HAProxy check if certain values exist in the POST body (E.g. the notification url value "pingpong"), and if this is the case, route the traffic to an endopint I will specify in the configs.

POST /someURL/file.jsp HTTP/1.1
Host: 10.43.90.190:80
Content-Type: application/json
Connection: keep-alive
Accept: */*
Content-Length: 256

{"Info": {"groupName":"thisgroup1","Id":"M1234R456","id2":"TUP1234",
    "countryCode":"USA","carrierCode":"USAIC","e164Address":"123456768789",
    "notificationURL":"http:\/\/www.pingpong.com\/notify",
    "timestamp":"2014-03-04T17:33:30.000Z"}}

Is there any way to use an acl to search for the content, "pingpong" in the request body, and based on this value, I would route it appropriately?

Thanks!

Adulterant answered 24/4, 2014 at 4:28 Comment(0)
U
9

This can be done using a simple Access Control List (ACL). However, This wasn't possible up until Haproxy 1.6 (October 2015), where you can include this option in your frontend:

option http-buffer-request

This option gives Haproxy access to the body. Then you can use req.body to access the body. Example:

frontend http-in
             bind *:80
             option http-buffer-request
             acl redirect_pingpong req.body -m reg [insert your regular expression here]
             use_backend pingpong_backend if redirect_pingpong

             default_backend web_bk

And then go on to define your backends.

Further information on accessing body content can be found here and information about ACLs can be found here.

Unni answered 15/6, 2016 at 8:16 Comment(5)
Can you give an example for " [insert your regular expression here]". For example, if you want to match "hello" in localhost?userid=helloNightie
"[insert your regular expression here]" is just the regular expression. with no brackets, quotes or anything. Matching "hello" would require "acl redirect_pingpong req.body -m reg hello"Nightie
the links are dead.Callus
Docs for option http-buffer-request haproxy.com/documentation/hapee/latest/onepage/…Illjudged
HAProxy ACLs docs haproxy.com/documentation/hapee/latest/onepage/#7Illjudged
B
0

there is possibility to work it out in nginx / tengine but i still not found that in HAProxy, still if it is possible for you to switch to tengine/nginx, you can use nginx_http_lua_module. Still i must warn you, it have some performance issues that i have not solved yet,

Bise answered 5/8, 2015 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.