HAProxy community

Haproxy redirect multiple webserver

#1

Hi all,

I’m looking for a possible configuration for haproxy to redirect http requests to multiple web servers. In particular, if the request is http://193.206.112.10 it is diverted to the 10.200.1.1 and 10.200.1.2 servers, if the http request is http://193.206.112.10/test it is diverted to the server 10.200.1.3

#2

Just define two backends, the first one with the two servers, and a second one for the third server. Then just use ACL’s based on the path sample to use_backend the proper one.

#3

I didn’t understand how to do it, could you tell me how to do it? Sorry but I’m not very good with ACL

#4

If you only have two conditions i.e if the path is /test, use one backend else use the other, it can be achieved as:

acl is_test path -i -m beg /test
use_backend be_test if is_test

This is using the acl is_test if the URL path is beginning with /test. If this condition is true, it will select the be_test backend where you can put the 10.200.1.3 server as backend. Otherwise, the default backend will be selected where the backend servers are 10.200.1.1 and 10.200.1.2

#5

Ok I try this solution. Thx

#6

This is my haproxy.cfg, but

frontend Local_Server
bind *:80
mode http
option httplog
option http-server-close
acl is_admin path_beg -i /administrator
redirect prefix http://172.22.80.16 if is_admin
acl is_ufsoit path -i -m beg /ufsoit
use_backend be_ufsoit if is_ufsoit
default_backend My_Web_Servers

backend My_Web_Servers
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server web1 172.22.80.17:80
server web2 172.22.80.18:80
server web3 172.22.80.19:80

backend be_ufsoit
mode http
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1rnHost:localhost
server web4 172.22.80.20:80

When i call http://my_name_server/ufosit an error occur.
I think because in the server 172.22.80.20 there is no subdirectory called ufsoit

#7

What is the error?

#8

Page not found 404

#9

Any suggest?

#10

So you need to strip off the first part of the path in the be_ufsoit backend?

Try this:

backend be_ufsoit
  …
  reqrep "^([^ :]*) /ufsoit/(.*)" "\1 /\2"
#11

I used the following rule and the routing works correctly to the right server.

reqrep ^([^\ ]*\ /)web1[/]?(.*)     \1\2

But the page is loaded without a style sheet and images

#12

Having seen much pain with subfolder setups like this, you are in for a world of hurt if the backend isn’t expecting traffic at that subfolder because it’s probably sending back links to assets at /, not /ufsoit/