# %\[\] in use-server

**URL:** <https://discourse.haproxy.org/t/in-use-server/3529>\
**Category:** Help!\
**Created:** [February 18, 2019, 4:11pm UTC](https://discourse.haproxy.org/t/in-use-server/3529 "2019-02-18T16:11:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![idiot](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@idiot](https://discourse.haproxy.org/u/idiot)\
**Post date:** [February 18, 2019, 4:11pm UTC](https://discourse.haproxy.org/t/in-use-server/3529/1 "2019-02-18T16:11:35Z")

</div>

👋

I’m trying to make servers “dynamic” by using a “templated” backend with servers pulled from a map

```auto
backend be_template
  server-template websrv 1-100 localhost:6666 check disabled
  use-server %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map,websrv0)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map) -m found }

```

However, the above doesn’t work, haproxy fails to start with the following error:

```auto
haproxy_1 | [ALERT] 048/160628 (1) : config : backend 'be_template' : unable to find server '%[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map,websrv0)]' referenced in a 'use-server' rule.
haproxy_1 | [ALERT] 048/160628 (1) : Fatal errors found in configuration.

```

I wonder if `%[]` syntax is allowed in `use-server` like in `use_backend`?

* * *

HAProxy version that I’m using is 1.9.

Full config:

```auto
global
  log /dev/log local0
  log /dev/log local1 notice
  chroot /var/lib/haproxy
  # TODO
  stats socket ipv4@127.0.0.1:9999 level admin
  # stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
  stats timeout 30s
  daemon

defaults
  log global
  mode http
  option httplog
  option dontlognull
  option forwardfor
  option http-keep-alive
  http-reuse safe
  timeout connect 5000
  timeout client 50000
  timeout server 50000

frontend http
  bind :::80 v4v6
  use_backend be_template

backend be_template
  server-template websrv 1-100 localhost:6666 check disabled
  use-server %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map) -m found }

```

---

<div class="post-metadata">

**Author:** ![idiot](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@idiot](https://discourse.haproxy.org/u/idiot)\
**Post date:** [February 18, 2019, 4:26pm UTC](https://discourse.haproxy.org/t/in-use-server/3529/2 "2019-02-18T16:26:29Z")

</div>

I think [Dynamic server selection](https://discourse.haproxy.org/t/dynamic-server-selection/149/3?u=idiot) might be related. I thought about using a lua script to pick the server but it seems it’s not supported either.

---

<div class="post-metadata">

**Author:** ![idiot](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@idiot](https://discourse.haproxy.org/u/idiot)\
**Post date:** [February 18, 2019, 4:32pm UTC](https://discourse.haproxy.org/t/in-use-server/3529/3 "2019-02-18T16:32:46Z")

</div>

My use case is similar to [gitlab review apps](https://gitlab.com/help/ci/review_apps/index.md) – on some PRs I want to be able to start web apps each with its own subdomain which would register themselves with haproxy via a socket like this:

```auto
set server be_template/websrv<i> addr <app_host> port <app_port>
set server be_template/websrv<i> state ready
add map /usr/local/etc/haproxy/domain2server.map <COMMIT_REF_SLUG>.domain.com websrv<i>

```

---

<div class="post-metadata">

**Author:** ![idiot](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@idiot](https://discourse.haproxy.org/u/idiot)\
**Post date:** [February 18, 2019, 5:26pm UTC](https://discourse.haproxy.org/t/in-use-server/3529/4 "2019-02-18T17:26:40Z")

</div>

I’ve also asked this question on the mailing list: [https://www.mail-archive.com/haproxy@formilux.org/msg32843.html](https://www.mail-archive.com/haproxy@formilux.org/msg32843.html)

---

<div class="post-metadata">

**Author:** ![idiot](https://avatars.discourse-cdn.com/v4/letter/i/f04885/32.png) [@idiot](https://discourse.haproxy.org/u/idiot)\
**Post date:** [February 18, 2019, 5:43pm UTC](https://discourse.haproxy.org/t/in-use-server/3529/5 "2019-02-18T17:43:45Z")

</div>

Seems like it is not possible, haproxy [just does a `strcmp`](https://github.com/haproxy/haproxy/blob/3d95717b58be0902078e0693e383bc32ea3a715a/src/proxy.c#L714) without expanding what’s inside `%[]` …

~~The only option I have left now is to regenerate the whole config and reload haproxy or look for some other reverse proxy (traefik might be a good fit) for this particular use case.~~

But there’s a workaround: [https://www.mail-archive.com/haproxy@formilux.org/msg32845.html](https://www.mail-archive.com/haproxy@formilux.org/msg32845.html)

```auto
http-request set-dst %[req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map)] if { req.hdr(host),lower,map_dom(/usr/local/etc/haproxy/domain2server.map) -m found }

```
