Pegasus1985
Pegasus1985

Reputation: 186

Haproxy - 301/302 redirect URL1 to URL2 with all pathes

I'm a little bit lost atm. I try to implement a redirect within a complex HAproxy configuration. The goal is simple:

I'm totally unsure how to implement that without taking the rist of taking down production traffic.

What i would do:

redirect prefix http://123.domain.com code 301 if { hdr(host) -i api.domain.com }

Not usre if that would work.

redirect location https://www.mysites/v2/pages 302 if { hdr(host) -i api.domain.com }

It's hard to implement this wihtout taking a risk of an outage. Is there something who could know the answer?

Upvotes: 1

Views: 6515

Answers (1)

dcorbett
dcorbett

Reputation: 442

First I would recommend that you have a development and/or staging environment configured to test any changes before you make them.

To do a 301 redirect of all traffic coming from 123.domain.com to api.domain.com you can use the following

http-request redirect prefix http://api.domain.com code 301 if { hdr(host) -i 123.domain.com }

If you wanted HAProxy to connect to the api.domain.com backend on the users behalf and mask the hostname itself you would add a backend for api.domain.com and then create a use_backend rule. Keep in mind that with the below there are no 301/302 redirects performed, instead HAProxy makes the connection on behalf of the client.

Something like this would work:

use_backend api.domain.com if { hdr(host) -i 123.domain.com }

Then within api.domain.com backend you can update the Host header.

backend api.domain.com
    http-request set-header Host api.domain.com
    server api1 api.domain.com:80 check

Upvotes: 1

Related Questions