Configure HAProxy to include host headers for different backends

I'm not sure if the following will work, and can't test right now, but maybe this is still helpful. (I'll have a look again later with some more time):

Solution 1:

backend nodes
    mode http
    balance roundrobin
    option forwardfor
    http-request set-header Host node1.myapp.mycompany.com if { srv_id 1 }
    http-request set-header Host node2.myapp.mycompany.com if { srv_id 2 }
    server web01 node1.myapp.mycompany.com:80
    server web02 node2.myapp.mycompany.com:80

Solution 2:

Note This one is officially not recommended. Read the comment on http-send-name-header: it has been reported that this directive is currently being used as a way to overwrite the Host header field in outgoing requests; while this trick has been known to work as a side effect of the feature for some time, it is not officially supported and might possibly not work anymore in a future version depending on the technical difficulties this feature induces... [Credits to rustyx.]

backend nodes
    mode http
    balance roundrobin
    option forwardfor
    http-send-name-header Host
    server node1.myapp.mycompany.com node1.myapp.mycompany.com:80
    server node1.myapp.mycompany.com node2.myapp.mycompany.com:80

I want to complement gf_ answer. So the idea of his answer is to add a custom Host header which value is the source hostname itself. In solution one, basically, he set the header manually with a condition from srv_id thing.

The second on shortcut the first solution by sending the name of the host as a Host header.

But in his solution, he doesn't mention that it only set-header, if the header is already there it can't replace the existing header, so if your case need to replace Host header, you can do somethinghing like this

backend nodes
    mode http
    balance roundrobin
    option forwardfor
    http-request replace-header Host node1.myapp.mycompany.com node1.myapp.mycompany.com if { srv_id 1 }
    http-request replace-header Host node2.myapp.mycompany.com node2.myapp.mycompany.com if { srv_id 2 }
    server web01 node1.myapp.mycompany.com:80
    server web02 node2.myapp.mycompany.com:80