Pegasus1985
Pegasus1985

Reputation: 186

HAproxy - How to forward traffic to backend by a matching header?

E.g. the header contains: X-DEMO-HOST-VERSION: test - how do i need to setup the haproxy config to forward to a specific backend? Can this be done within the backend or in the frontend?

Regarding to the documentation i'm a little bit confused.

Upvotes: 3

Views: 7993

Answers (1)

qwsj
qwsj

Reputation: 456

You need a check header (hdr in haproxy) value via ACL. Try it:

frontend header_front
    bind *:80
    mode http
    option forwardfor if-none
    acl demo_host_version hdr(X-DEMO-HOST-VERSION) -i test
    use_backend test_backend if demo_host_version
    default_backend prod_backend

backend test_backend
    ...

backend prod_backend
    ...

Test for you header:

    acl demo_host_version hdr(X-DEMO-HOST-VERSION) -i test
    use_backend test_backend if demo_host_version

The logic: IF X-DEMO-HOST-VERSION=test use test_backend ELSE use prod_backend


UPD: If u need acl for path in URL (example.com/test/):

    acl demo_host_path path_beg /test/
    use_backend test_backend if demo_host_path

If u need use a two ACL in if statement use it:

    acl demo_host_version hdr(X-DEMO-HOST-VERSION) -i test
    acl demo_host_path path_beg /test/
    use_backend test_backend if demo_host_path demo_host_version

UPD for check URL:

Subdomain check. If u domain test.example.com or test.abc.com, try it:

acl host_sub_domain hdr_beg(host) -i test

For full domain check, eg test.example.com, try it:

acl host_full_domain hdr(host) -i test.example.com

Upvotes: 3

Related Questions