Create a cookie with a value of the requested subdomain

Create a cookie with a value of the requested subdomain

Community version 2.0.14-lp152.1.2.x86_64
openSUSE leaf 15.2

Working with a new system that is multi-tenanted where externally the customers use a url of customerX.mysite.com only rather than a number its a name
Bob.mysite.com
Fred.mysite.com
Charlotte.mysite.com

The backend servers are all balanced using leastconn and any customer can use any backend server.

At present we have a complicated an clunky way of redirecting each user to the correct database however this could be set as a session cookie

I have been asked to insert a cookie that has the subdomain (bob, fred, etc.) as a value which could then function as the identifying cookie

Currently I have tried creating a variable within the listen statement and then using that within the backend. This creates the cookie DBNAME with a / path but as I have done it so far, there is no value

frontend mysite
bind *:443 ssl crt /etc/haproxy/mydomain.pem alpn h2,http/1.1
mode http
option httpclose
http-request set-var(req.subdomain) req.hdr(host),lower,regsub(.mysite.com$,) if { hdr_end(host) -i .mysite.com
default_backend myservers

backend myservers
mode http
cookie SERVERID insert indirect nocache
http-response add-header Set-cookie DBNAME=%[var(req.subdomain)];\ path=/
server myserver1 192.168.0.1:8443 cookie srv1 ssl verify none check
server myserver2 192.168.1.1:8443 cookie srv2 ssl verify none check
… (10 total my servers)

Why no value? I have used the same set-var statements in the past but this time nothing.

Thanks

Tim

Trying more ideas I ended up with trying a simple process

    http-response add-header Set-cookie DBNAME=whatdb;\ path=/

And that does indeed create a cookie called DBNAME with a value of “whatdb” and a path of /
Excellent, so why does the variable not work? Have I messed up the syntax ?

Proving that the variable works as part of a URL I tried:

     http-request redirect code 302 location https://host.mysite.com:8443/iqx/?db=%[var(req.subdomain)] if is_app

And that works well if somewhat inelegant.

Ideas?

Thanks

Tim

That was fun :slight_smile: A long journey

From the manual we can see that there are 5 flavours of variable

“proc” : the variable is shared with the whole process
“sess” : the variable is shared with the whole session
“txn” : the variable is shared with the transaction (request and response),
“req” : the variable is shared only during request processing,
“res” : the variable is shared only during response processing.

It seems that there are greater boundaries than is intimated above - in that a transaction or session based variable (let alone a request or response) is only allowed within one frontend or backend element. If you want to use a variable created in one frontend (or backend) in a different section then the variable must be a Process based variable

By changing to creating a variable based on “proc” then it works as required.

frontend mysite
bind *:443 ssl crt /etc/ssl/cert/mydomain.pem alpn h2,http/1.1
mode http
option httpclose
http-request set-var(proc.subdomain) req.hdr(host),lower,regsub(.mysite.com$,) if { hdr_end(host) -i .mysite.com
default_backend myservers

backend myservers
mode http
http-response add-header Set-cookie DBNAME=%[var(proc.subdomain)];\ path=/
cookie SERVERID insert indirect nocache
server myserver1 192.168.0.1:8443 cookie srv1 ssl verify none check
server myserver2 192.168.1.1:8443 cookie srv2 ssl verify none check
… (10 total my servers)

Hope that helps someone else in the future

Tim