Dynamic backend server port from request

Hi folks, I’m trying to come up with a scheme by which the backend server port is selected based on some parameters of the request. For example, some requests might go to backend foo, server port M, while others might go to backend foo, server port N. Rather than have backends foo_M and foo_N, I’d like it to be dynamic.

The backend selection works fine, but when I try to define the server with the dynamic port, I get a syntax error. I’ve tried numerous ways of interpolating the string in the server line, most of which I’m embarrassed to even show because they are obviously not right. Here’s an example of what I’m trying to do (HAProxy 1.6+):

backend foo
  server foo1[var(txn.service_port)]

Is there a proper way to accomplish this? Is it just not possible to have a dynamic port due to some internal operational characteristics of HAProxy?

Failing that, I’m aware that HAProxy will use the original dst_port as the server port if it is omitted. Is there some way to override the value of dst_port before calling use_backend?

I have an idea on how to make this work using a dummy bind address and HTTP redirects, but that is undesirable for obvious reasons. Thank you in advance for any insight you can offer into better options.

1 Like

I’m into the very same problem.
I know this thread is dead, but are there any updates on this?

My specific scenario is that I have a server running different endpoints in different ports:


I want a frontend server to group all these endpoints together in a single port.

I know I can create a backend for each server and map them accordingly. But it makes sense for me to have a way to map the ports the same way I map the paths, since they are all in the same server.

I don’t see how this could possibly work with haproxy, the internal architecture does not permit this.

Thank you for your reply and the info.

At least, I stop trying to figure out how to do this. :slight_smile: