Skip to content

Instantly share code, notes, and snippets.

@ledangtuanbk
Last active February 21, 2022 17:39
Show Gist options
  • Save ledangtuanbk/2055dd636a785e84b16b3650fa70e1e6 to your computer and use it in GitHub Desktop.
Save ledangtuanbk/2055dd636a785e84b16b3650fa70e1e6 to your computer and use it in GitHub Desktop.
Haproxy redirect request base on body
Based on this guide
https://stackoverflow.com/questions/23259843/how-to-route-traffic-reverse-proxy-with-haproxy-based-on-request-body
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
We use regex to redirect request
Example
frontend kifarunixlb
bind localhost:8088
acl redirect_app2 req.body -m reg \"data\":.*\"test1\"
use_backend app2 if redirect_app2
default_backend app1
option forwardfor
backend app1
balance roundrobin
server app01 localhost:8080 check
backend app2
balance roundrobin
server app02 localhost:8081 check
when body request contain "data":"test0", the request will be processed by server02
remember string to hex upper case
check in both body and header
acl redirect_app2 req.payload(0,0),hex -m sub 7573657231 // 7573657231 -> user1
Cache-Control: no-cache -> ok
Content-Type: application/json
user: user2
or we can use pare keyvalue
user:user1 // attendtion remove space between key and value
the best way is use header
acl redirect_app2 hdr_val(user_id) eq my_user_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment