0

This is kind of a weird complicated question. Context: I have a bunch of docker containers that need to be routed to from haproxy dynamically. They are each running on different ports on the machine, and are stored in environment variables like this:

a=9873
b=9874
c=9875

These are available to the haproxy server. The request path that comes in will be in the form like this example:

/api/a/action

From that, the taks is as follows:

  • The /api needs to be removed from the path.

  • The /a refers to the service, so the environment variable for a needs to be retrieved to get the port of the server

  • The request needs to be routed to localhost:9873/a/action where the port, 9873, is the environment variable that is the value from the path in the beginning (after removing /api) and then the path is simply appended onto the request (which is the /a/action that remains after removing the /api.

My current config looks like this:

backend api  
  reqrep ^([^\ ]*\ /)api[/]?(.*) \1\2    
  server api_server localhost:9871

All this config is doing is removing the /api from the path of the request and sending it to a static port, 9871. *I need this port to be the value held by the environment variable of the same name as the first element in the path (the /a above) and the rest (passing the remaining path) is already working.*

I also would like to be able to get the environment variable of the name prefix_a, where the path will have the name /a, but I need to prepend one common prefix prefix_ to get the environment variable. This can be a separate question or search though, unless it's simple to just put that into the solution.

Please let me know if I can clarify or give more information that might help solve the problem.

(I've done a heck a lot of googling. Here are some related urls but not quite the answer I need:

S. Jacob Powell
  • 396
  • 2
  • 9
  • I was hoping for like a `getenv(\2)` or something, because I need to get the environment variable that corresponds to the value in \2 (after getting the first part of the path from \2 via regex or something). I guess something like `reqrep ^([^\ ]*\ /)api[/]?(.*)[/](.*) \1\2\3` Which might capture the first part of the path for use in getting the environment variable. – S. Jacob Powell Mar 23 '18 at 01:02
  • I ended up using zuul for this, which is a configurable reverse proxy. My set up uses both zuul and haproxy as microservices. – S. Jacob Powell Jul 03 '18 at 15:20

0 Answers0