How to replace domain in haproxy request?

is it possible to change/rewrite/replace the domain name in the request before you pass it through to the backend server?

I have 1 frontend:

frontend https-dev
        maxconn 2000
        option forwardfor header X-Real-IP
        http-request set-header X-Real-IP %[src]
        default_backend be-dev-cdn

        # BEGIN CORS
        http-response set-header Access-Control-Allow-Origin "*"
        http-response set-header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization, JSNLog-RequestId, activityId, applicationId, applicationUserId, channelId, senderId, sessionId"
        http-response set-header Access-Control-Max-Age 3628800
        http-response set-header Access-Control-Allow-Methods "GET, DELETE, OPTIONS, POST, PUT"
        # END CORS

        bind *:443 ssl crt domain.pem
        reqadd X-Forwarded-Proto:\ https
        # set X-SSL in case of ssl_fc <- explained below
        http-request set-header X-SSL %[ssl_fc]

And 1 backend with multiple hosts:

backend be-dev-cdn
        balance roundrobin
        server dev-cdn01.domain.com 192.168.101.1:80 check
        server dev-cdn02.domain.com 192.168.101.2:80 check
        server dev-cdn03.domain.com 192.168.101.3:80 check
        server dev-cdn04.domain.com 192.168.101.4:80 check
        server dev-cdn05.domain.com 192.168.101.5:80 check

The user would input the domain www.domain.com and I would then rewrite/replace this domain to a new domain name based on the backend server to which the request is forwarded. For example: www.domain.com → haproxy → backend server dev-cdn05.domain.com; rewrite the request.

My goal is that I would just like to route the requests through haproxy server but not the response

Is this possible with haproxy?

Hello,

In your backend section, you can add

http-request set-header Host otherdomain.com

If needed you can add a condition, for example :

http-request set-header Host otherdomain.com if { req.hdr(host) -i mydomain.com }

Hi,

yes…this works if you are replacing 1 domain. If you look at my example I have configured 5 backend servers and would like to change the domain to the “domain” of the selected backend server.

Example:

selected backend server: dev-cdn01.domain.com change domain to dev-cdn01.domain.com
selected backend server: dev-cdn02.domain.com change domain to dev-cdn02.domain.com
selected  backend server: dev-cdn03.domain.com change domain to dev-cdn03.domain.com
selected backend server: dev-cdn04.domain.com change domain to dev-cdn04.domain.com
selected backend server: dev-cdn05.domain.com change domain to dev-cdn05.domain.com

BR

Hello,

I don’t know if it’s possible with different servers within the same backend. I mad some test (with : http-request set-header Host %s) and when Haproxy modify the headers, it seems it doesn’t know yet the server it will use.

Withe several backends named regarding the hos you want you can do

http-request set-header Host %b

Maybe with dynamic mapping but not very sure.

if you change the server name to match the domain you are sending it to, then you can use:

http-send-name-header  host

which will send the server’s “name” as the “Host” header.

1 Like

http-request set-header Host %b

That will set the host to the name of the backend, which is “be-dev-cdn”

%s will give you the servername, but I"m not sure if there is a way to get the hostname of the server line.

That’s why a said he has to rename backend.

But i was not aware of the http-send-name-header option. This seems the right way to do it.