HAProxy community

Rerouting in HAPROXY

Due to business needs we need to reroute the incoming requests from “rpi.flipcart.net” to “txg.fipcart.net”. I have configured the haproxy as

"frontend http
bind *:80
bind *:443 ssl crt /etc/haproxy/certs/wildcard.pem
option http-server-close
option http-keep-alive
mode http
acl rpi hdr(host) -i rpi.flipcart.net
use_backend rpihttp1 if rpi

backend rpihttp1
balance roundrobin
server prod-webrpi1 10.XXX.XXX.XX:80 check inter 30s
server prod-webrpi2 10.XXX.XXX.XX:80 check inter 30s"

Application on web servers installed to server “txg.fipcart.net”. Not sure at how and what level (fronend/backend) reroute need to be implemented.

“reqirep ^Host: rpi.flipcart.net Host:txg.flipcart.net if rpi”

Anyone recommend to use the above directive?

This should do it (if you really only need to rewrite the Host header):

http-request set-header Host txg.fipcart.net if hdr(host) -i rpi.flipcart.net

This has to be the part of frontend right?

"frontend http
bind *:80
bind *:443 ssl crt /etc/haproxy/certs/wildcard.pem
option http-server-close
option http-keep-alive
mode http
acl rpi hdr(host) -i rpi.flipcart.net
http-request set-header Host txg.fipcart.net if hdr(host) -i rpi.flipcart.net
use_backend rpihttp1 if rpi

backend rpihttp1
balance roundrobin
server prod-webrpi1 10.XXX.XXX.XX:80 check inter 30s
server prod-webrpi2 10.XXX.XXX.XX:80 check inter 30s"

Yes, this would be in the frontend.

This is for rewriting the Host header though.

I’m not sure I understood what you mean by rerouting though, so I don’t know if that is what you actually need, or whether you need something else entirely.

Can you confirm all the request for both rpi.flipcart.net and txg.fipcart.net need to go to your servers prod-webrpi1 and prod-webrpi2 in the rpihttp1 backend?

I probably using the wrong terminology here, let me explain the scenario in detail.

We had an application running on IIS with an end point “rpi.flipcart.net” and configured the haproxy as load balancer and all dependencies use the end point: rpi.flipcart.net. Upto here everything looks pretty simple.

Now the business decided to change the end point from “rpi.flipcart.net” to “txg.fipcart.net”, we made the required changes on IIS. This also required to update the end point in all dependencies, which requires a maintenance window of couple of minutes. Unfortunately, the dependencies are very critical for the business and we can’t afford a downtime.

As a workaround, we are thinking of rerouting all incoming web requests “rpi.flipcart.net” --> “txg.fipcart.net” on HAPROXY.

Ok, but why do you think you need the use_backend conditioned based on the host header.

Why can’t you just route ALL the traffic your recieve to your backend:

frontend http
bind *:80
bind *:443 ssl crt /etc/haproxy/certs/wildcard.pem
option http-server-close
option http-keep-alive
mode http
http-request set-header Host txg.fipcart.net if hdr(host) -i rpi.flipcart.net
default_backend rpihttp1

backend rpihttp1
balance roundrobin
server prod-webrpi1 10.XXX.XXX.XX:80 check inter 30s
server prod-webrpi2 10.XXX.XXX.XX:80 check inter 30s

I don’t understand what you want to achieve with conditional use_backend directive.

If the configuration you provided is incomplete and there is more to it than you shared, just match both host headers instead:

frontend http
bind *:80
bind *:443 ssl crt /etc/haproxy/certs/wildcard.pem
option http-server-close
option http-keep-alive
mode http
acl rpi hdr(host) -i rpi.flipcart.net
acl rpi hdr(host) -i txg.flipcart.net
http-request set-header Host txg.flipcart.net if hdr(host) -i rpi.flipcart.net
use_backend rpihttp1 if rpi

backend rpihttp1
balance roundrobin
server prod-webrpi1 10.XXX.XXX.XX:80 check inter 30s
server prod-webrpi2 10.XXX.XXX.XX:80 check inter 30s

And the host rewriting part has already been covered.

Yes, as you expected we have multiple frontends and backends in the confguration.

Then you have the answer above.

1 Like

Thank you for your time and sharing knowledge with community