Dynamic backend server based on request host header

Hello !

I have a pool of servers with a dedicated private dns record to each server:
example1.test.com: 172.10.0.1
example2.test.com: 172.10.0.2

I want HAProxy to be able to receive requests with public host: example1-ext.test.com or example2-ext.test.com and forwarded them to the corresponding private dns record.

example1-ext.test.com → HAProxy magic → example1.test.com

Plus, I am not able to know in advance if an example3-ext.test.com will exists, but I still want HAProxy to try to forward it to example3.test.com

It seems possible to do it with Nginx:

location / {
    # headers...

    resolver 192.0.2.1;

    # A regex to get the first part (hostname) from the Host header
    if ($http_host ~* "([a-z0-9-]+)(\.[a-z0-9-]+)*") {
        # Save a captured part from the regex to a variable
        set $redirect_hostname $1;
        # Pass the request to a desired backend
        proxy_pass   http://$redirect_hostname.private.mydomain.example$request_uri;
    }
}

Source

Thanks :slight_smile: