6

When using HAProxy for virtual hosting, I can see how to use the Host from the header at the front end to decide what backend to route to. However, is it possible to make the back end be a URL which includes a path (not unlike what you would do with apache or nginx when setting up virtual hosting).

http://www.techrawr.com/tag/haproxy/ - shows most of it. But what if the back ends were on the one server but with backend1 and backend2 as the servers?

4
  • did you manage to work Haproxy with backend urls containing path?
    – x__dos
    Jun 24, 2013 at 15:11
  • No - I use nginx heavily for places where that is needed, so it is a non issue. And it seems counter to the architecture of haproxy. Jun 26, 2013 at 7:34
  • I would like to see a HAProxy solution for this as well.
    – Developr
    Feb 28, 2014 at 19:45
  • It looks like there is a solution with reqirep (see stackoverflow.com/a/22257151/360782). I haven't tried it yet.
    – Paul Lynch
    Dec 15, 2017 at 19:41

3 Answers 3

7

I think you're looking for something like this in order to balance to different servers based on the URL:

frontend http-farm
    bind 0.0.0.0:80
    acl app1web     hdr_beg(host) -i app1  # for http://app1.domain.com
    acl app2web     hdr_beg(host) -i app2  # for http://app2.domain.com

acl msg-url-1 url_reg ^\/path/games/.*
acl msg-url-2 url_reg ^\/path/photos/.*
acl msg-url-3 url_reg ^\/path/mail/.*
acl msg-url-4 url_reg ^\/path/wazap/.*

use_backend games  if  msg-url-1 app1web
use_backend photos if  msg-url-2 app2web
use_backend mail if .....



backend games
    option httpchk GET /alive.php HTTP/1.1\r\nHost:\ app1.domain.com
    option  forwardfor
    balance roundrobin
    server  appsrv-1  192.168.1.10:80  check inter 2000 fall 3
    server  appsrv-2  192.168.1.11:80  check inter 2000 fall 3

backend photos
    option httpchk GET /alive.php HTTP/1.1\r\nHost:\ app2.domain.com
    option  forwardfor
    balance roundrobin
    server  appsrv-1  192.168.1.13:80  check inter 2000 fall 3
    server  appsrv-2  192.168.1.14:80  check inter 2000 fall 3

Source: Haproxy ACL for balance on URL request

2
  • what does this line option httpchk GET /alive.php HTTP/1.1\r\nHost:\ app2.domain.com do ? Jan 2, 2021 at 18:18
  • @Ciastopiekarz It's a health check. If the backend doesn't respond to that request, HAProxy will stop directing traffic to it.
    – djsumdog
    Jan 2, 2021 at 23:09
-1

The "server" directive in HAProxy supports a number of parameters, and one option is "redir" parameter. You can use it to send an HTTP 302 redirect back to the client - and have the client talk to the backend (this will not proxy though):

Example:

backend www_example_com

balance roundrobin

server Backend1 10.0.0.1:80 redir http:// www.example.com/backend1

server Backend2 10.0.0.1:80 redir http:// www.example.com/backend2

1
  • 1
    hmm but that is a redirect - so the client will then talk directly to the backend - not via HAproxy after the redir has occurred? Mar 29, 2010 at 0:22
-2

If only HA proxy has a public port then redirect to backend would not be a good solution For this you may want the ha proxy to do the work as it suppose to, altough I'm not sure if it is a good practice to modify the path depending on the result of the loadbalance.

I posted my solution to stackoverflow under a similar question, check it out for more information Detailed answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .