0

I'm setting up HAProxy and I need to route based on API versions. The issue is that V2 doesnt have the prefix embedded in the router while v1 does. Here is an example.

v1 app route = server/v1/path v2 app route = server/path

I need to configure HA so it will route server/v2/path to /server/path while returning a 200 and not a 302 status code. I do not want api users to know anything different than /server/v2/path

This is my config, everything defaults to v2.

frontend api
   bind *:80
   stats uri /ls
   acl url_v1 path_beg /v1
   use_backend api_v1 if url_v1
   default_backend api_v2

backend api_v2
   balance roundrobin
   server v1 ip:80 check

backend api_v1
   server v1 ip:80 check
Ray Hughes
  • 141
  • 1
  • 6

1 Answers1

2

Assuming HAProxy 1.6 or later, this should work:

backend api_v2
   balance roundrobin
   server v1 ip:80 check
   http-request set-path %[path,regsub(^/api/v2,/api)] 

This rewrites the path before sending the request to the server. It doesn't redirect, and the HAProxy log will still show the actual path the client requested.

Michael - sqlbot
  • 21,988
  • 1
  • 57
  • 81