18

A load balancer is set up with two back ends.

The request URI will look like the following:

http://example.com/answers/submit
http://example.com/tag-02/answers/submit

How can I configure haproxy in such a way that the request is sent to one or the other of the two back ends, depending on the format of the request URI? The only difference between the requests is /tag-02/ in the request URI.

A haproxy config file for this with a bit of explanation would be much appreciated, since Iā€™m new to haproxy.

TRiG
  • 1,167
  • 2
  • 13
  • 30
Autodidact
  • 385
  • 2
  • 4
  • 13

2 Answers2

18

You want to use ACLs:

backend be1 # this is your default backend
  ...
backend be2 # this is for /tag-02 requests
  ...

frontend fe
  ...
  default_backend be1
  acl url_tag02 path_beg /tag-02
  use_backend be2 if url_tag02

Section 7 of the HAProxy configuration guide has the details on ACLs, but you have to know the magic use_backend incantation hidden in section 4 of the guide to know what to do with the ACLs.

Ray Baxter
  • 103
  • 4
natacado
  • 3,317
  • 28
  • 27
  • 2
    While the declared ACL style is generally more readable and reusable, you can, if you prefer, include the ACL declaration in the `use_backend` directive itself, with `use_backend be2 if { path_beg /tag-02 }`. ā€“ womble Apr 19 '18 at 04:59
1

To provide a better example to the answer above , below is a configuration example.

frontend https-in
   bind *:443 ssl crt /etc/ssl/server.pem
   mode http
   redirect scheme https if !{ ssl_fc }

   acl uri_help path_beg /help
   use_backend help if uri_help

backend help
    balance     roundrobin
    server      help yourbackendserver.com check
  • thanks. really helpfull. has anyone tested this one?? pasting another option below. [enter image description here](https://i.stack.imgur.com/eOKy0.png) ā€“ waqas ahmed Apr 21 '22 at 22:17