Search code examples
httpurl-rewritingheaderhaproxyhost

haproxy rewrite uri keep backend header host


By making a rewrite in Haproxy 1.8, I need to make a URI redirect to another domain (host), but keep header host in request.

Example:

www.mysite.com/api -> 104.4.4.4/api (rw) -> result www.mysite.com/api (response)

I made a lot of tests with some parameters of HA, and I managed to obtain some succes, but with one problem.

This is my actual scnenario

backend site1
acl path_to_rw url_beg /api
acl mysite hdr(host) -i www.mymainsite.com

http-request set-header Host www.mymainsite.com if mysite path_to_rw
reqirep ^Host Host:\ host_to_forward/api if mysite path_to_rw
cookie SERVERID insert indirect nocache maxlife 1h
server site1 myhost:80 check cookie site1

My backend is a IIS server, and my rewrite works. But, I get error bellow:

"HTTP Error 400. The request hostname is invalid"

It seems that my backend does not accept the headerhost tha i send. Have somebody already had this problem ?


Solution

  • I managed to fix this problem, with a simple combination between acl´s and "use backend" directive.

    e.g:

    Header host: www.mysite.com

    Path to aplication in another origin /api

    acl myhost hdr(host) -i www.myhost.com
    acl path_api url_reg -i  /API(.*)
    use_backend be_origin_servers if myhost path_api
    
    backend be_origin_servers
    server myserver1 10.10.10.10 check cookie myserver1