# Dynamic backend server based on request host header

**URL:** <https://discourse.haproxy.org/t/dynamic-backend-server-based-on-request-host-header/8568>\
**Category:** Help!\
**Created:** [April 3, 2023, 1:27pm UTC](https://discourse.haproxy.org/t/dynamic-backend-server-based-on-request-host-header/8568 "2023-04-03T13:27:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![guanty](https://avatars.discourse-cdn.com/v4/letter/g/8e8cbc/32.png) [@guanty](https://discourse.haproxy.org/u/guanty)\
**Post date:** [April 3, 2023, 1:27pm UTC](https://discourse.haproxy.org/t/dynamic-backend-server-based-on-request-host-header/8568/1 "2023-04-03T13:27:49Z")

</div>

Hello !

I have a pool of servers with a dedicated private dns record to each server:  
[example1.test.com](http://example1.test.com): 172.10.0.1  
[example2.test.com](http://example2.test.com): 172.10.0.2  
…

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

[example1-ext.test.com](http://example1-ext.test.com) → HAProxy magic → [example1.test.com](http://example1.test.com)

Plus, I am not able to know in advance if an [example3-ext.test.com](http://example3-ext.test.com) will exists, but I still want HAProxy to try to forward it to [example3.test.com](http://example3.test.com)

It seems possible to do it with Nginx:

```auto
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](https://serverfault.com/questions/291703/dynamic-backends-with-haproxy)

Thanks 🙂
