Search code examples
haproxy

HAProxy: change backend as path?


I am new to haproxy Is it possible for haproxy to using a backend when using the subpath? e.g.

test.app.com/a/  > appa.example.com:5000/
test.app.com/b/  > appb.example.com:5001/ 

so that

http://test.app.com/a/index.html >  appa.example.com:5000/index.html
http://test.app.com/b/testing/ > appb.example.com:5001/testing/

Solution

  • Yes, it is. ACLs for path, like path_beg and use_backend to route traffic as requested. In backends modify path with http-request set-path.
    Simplified example below:

    frontend http
      bind *:80
      acl url_a path_beg -i /a/
      acl url_b path_beg -i /b/
      use_backend backend-a if url_a
      use_backend backend-b if url_b
    
    backend backend-a
      http-request set-path %[path,regsub(^/a/,/,g)]
      server a appa.example.com:5000
    
    backend backend-b
      http-request set-path %[path,regsub(^/b/,/,g)]
      server b appb.example.com:5001