# Dynamic server selection

**URL:** <https://discourse.haproxy.org/t/dynamic-server-selection/149>\
**Category:** Help!\
**Created:** [March 2, 2016, 4:17pm UTC](https://discourse.haproxy.org/t/dynamic-server-selection/149 "2016-03-02T16:17:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![curtoni](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/curtoni/32/40_2.png) [@curtoni](https://discourse.haproxy.org/u/curtoni)\
**Post date:** [March 2, 2016, 4:17pm UTC](https://discourse.haproxy.org/t/dynamic-server-selection/149/1 "2016-03-02T16:17:17Z")

</div>

Hello, we are trying to set up a configuration were the server (not the backend) is dynamically selected by a LUA script.

We came up with this conf which is not working:

```
global
   lua-load choose-server.lua

defaults
    mode http

frontend http-in
    bind *:10001

    # The LUA variable is working in case we use to select the backend:
    #use_backend %[lua.choose_backend]
    default_backend dynamic

backend dynamic
    # Instead in this case it is not working
    server s1 %[lua.choose_server]

```

When starting the server we get this error:  
`[ALERT] 061/171533 (3343) : parsing [haproxy.conf:16] : 'server s1' : invalid address: '%[lua.choose_server]' in '%[lua.choose_server]'`

Is this not supported? Is there any other way to achieve the same result?

Thanks

---

<div class="post-metadata">

**Author:** ![ahayworth](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/ahayworth/32/11_2.png) [@ahayworth](https://discourse.haproxy.org/u/ahayworth)\
**Post date:** [March 21, 2016, 9:16pm UTC](https://discourse.haproxy.org/t/dynamic-server-selection/149/2 "2016-03-21T21:16:22Z")

</div>

Hi curtoni -

I think you need to spell out your servers in the backend, and use a different keyword to choose which specific one. Maybe something like:

```auto
backend dynamic
  use-server %[lua.choose_server]
  server s1 <ip>
  server s1 <ip>

```

I haven’t tested the config, so you may need to play around with it.

Here’s docs on the use-server keyword: [https://cbonte.github.io/haproxy-dconv/configuration-1.6.html#4.2-use-server](https://cbonte.github.io/haproxy-dconv/configuration-1.6.html#4.2-use-server)

Hope that helps!

- Andrew

---

<div class="post-metadata">

**Author:** ![aldahick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.haproxy.org/aldahick/32/395_2.png) [@aldahick](https://discourse.haproxy.org/u/aldahick)\
**Post date:** [January 26, 2018, 3:18pm UTC](https://discourse.haproxy.org/t/dynamic-server-selection/149/3 "2018-01-26T15:18:07Z")

</div>

I don’t mean to drag this thread up from the dead, but I have the same use case as curtoni, and I’m not sure that the solution that @ahayworth proposed is working properly. I’m using the following configuration on my testing instance:

```auto
# Some configuration options are redacted - the actual configuration is perfectly valid except for this issue.
global
    lua-load /root/nexus_route.lua
defaults
    mode http
backend eta
    use-server %[lua.get_server_for_nexus] if { path_beg -i /api/nexus/new /nexus/table /socket.io/nexus }
    # Server declarations

```

And I’m getting the following error when I start HAProxy: `config : backend 'eta' : unable to find server '%[lua.get_server_for_nexus]' referenced in a 'use-server' rule.`

Does anyone know if the post above is still valid for version 1.8.3?
