Split ip and port from URL and re-use in backend

Url’s are called by http://somewebsocket.domain.com/ on the frontend.
These ip and ports can differ.

We want to split the url and set variables for the ip and port (first and second parts after the domain) and be able to use these as a backend server.

Not separate blocks of backends but like we already do with incoming different ports.

For port only we have come up with something like this, but it also has to include the server-ip

On the backend something like :

frontend default

acl is_port path_reg /([0-9]+)$
use_backend range_http if is_port

backend range_http
http-request set-var(req.port) path,regsub(^.*/([0-9]+)$,\1,\1)
server lab[var(req.port)]

Which in fact also fails with a nice “error detected in backend ‘range_http’ while parsing ‘http-request set-var(req.port)’ rule : invalid arg 2 in converter ‘regsub’ : missing arguments (got 1/2), type ‘string’ expected.”

In short something like NGINX does like the example below :

server {
listen 80;
server_name websocket.domain.com;

location ~ ^/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/([0-9]+) {
    set $backend_ip $1;
    set $backend_port $2;
    proxy_pass http://$backend_ip:$backend_port;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # If you need to proxy WebSocket connections, include the following headers
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";


Can this be done with haproxy since this will make it really flexible and removes the need for hundreds of separate backends and a lot of reloading/reconfiguring.

Came up with below (simple conversion of nginx → haproxy)

frontend http_front
    bind *:80
    bind :::80 v6only

    acl path_dynamic path_reg ^/([a-zA-Z0-9.:]+)/([0-9]+)
    use_backend dynamic_backend if path_dynamic

backend dynamic_backend
    http-request set-var(txn.backend_ip) path,regsub(^/([a-zA-Z0-9.:]+)/([0-9]+).*,\1)
    http-request set-var(txn.backend_port) path,regsub(^/([a-zA-Z0-9.:]+)/([0-9]+).*,\2)

    http-request set-path %[path,regsub(^/[a-zA-Z0-9.:]+/[0-9]+,/)]

    server dynamic %[var(txn.backend_ip)]:%[var(txn.backend_port)] resolvers mydns resolve-prefer ipv4 check ssl verify none

    http-request set-header Host %[req.hdr(Host)]
    http-request set-header X-Real-IP %[src]
    http-request add-header X-Forwarded-For %[src]
    http-request add-header X-Forwarded-Proto https

But this error keeps haunting me :

error detected in backend ‘dynamic_backend’ while parsing ‘http-request set-var(txn.backend_ip)’ rule : invalid arg 2 in converter ‘regsub’ : missing arguments (got 1/2), type ‘string’ expected.


I think that set-dst and set-dst-port actions (with as server address) could help you to achieve that