# Rerouting in HAPROXY

**URL:** <https://discourse.haproxy.org/t/rerouting-in-haproxy/4236>\
**Category:** Help!\
**Created:** [August 26, 2019, 9:29pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236 "2019-08-26T21:29:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 26, 2019, 9:29pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/1 "2019-08-26T21:29:20Z")

</div>

Due to business needs we need to reroute the incoming requests from “[rpi.flipcart.net](http://rpi.flipcart.net)” to “[txg.fipcart.net](http://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](http://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](http://txg.fipcart.net)”. Not sure at how and what level (fronend/backend) reroute need to be implemented.

---

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 26, 2019, 9:30pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/2 "2019-08-26T21:30:27Z")

</div>

“reqirep ^Host: [rpi.flipcart.net](http://rpi.flipcart.net) Host:txg.flipcart.net if rpi”

Anyone recommend to use the above directive?

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 26, 2019, 9:33pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/3 "2019-08-26T21:33:44Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 26, 2019, 9:45pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/4 "2019-08-26T21:45:09Z")

</div>

> [@lukastribus](#):
>
> http-request

This has to be the part of frontend right?

---

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 26, 2019, 9:50pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/6 "2019-08-26T21:50:20Z")

</div>

> [@chmod1986](#):
>
> I have added the http-request directive under frontend, hope that works.
> 
> "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://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"

"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://rpi.flipcart.net)  
http-request set-header Host [txg.fipcart.net](http://txg.fipcart.net) if hdr(host) -i [rpi.flipcart.net](http://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"

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 26, 2019, 9:54pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/7 "2019-08-26T21:54:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 27, 2019, 4:39pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/8 "2019-08-27T16:39:19Z")

</div>

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](http://rpi.flipcart.net)” and configured the haproxy as load balancer and all dependencies use the end point: [rpi.flipcart.net](http://rpi.flipcart.net). Upto here everything looks pretty simple.

Now the business decided to change the end point from “[rpi.flipcart.net](http://rpi.flipcart.net)” to “[txg.fipcart.net](http://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](http://rpi.flipcart.net)” --\> “[txg.fipcart.net](http://txg.fipcart.net)” on HAPROXY.

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 27, 2019, 5:19pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/9 "2019-08-27T17:19:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 27, 2019, 5:58pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/10 "2019-08-27T17:58:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 27, 2019, 6:43pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/11 "2019-08-27T18:43:06Z")

</div>

Then you have the answer above.

---

<div class="post-metadata">

**Author:** ![chmod1986](https://avatars.discourse-cdn.com/v4/letter/c/b5e925/32.png) [@chmod1986](https://discourse.haproxy.org/u/chmod1986)\
**Post date:** [August 29, 2019, 4:19pm UTC](https://discourse.haproxy.org/t/rerouting-in-haproxy/4236/12 "2019-08-29T16:19:29Z")

</div>

Thank you for your time and sharing knowledge with community
