Tomcat subdomains and path redirection


I’m new to HAProxy and am trying find some assistance with a configuration problem. I need to create a configuration that will allow me to dynamically proxy a Tomcat application Context path to a subdomain. The subdomain/path can not be hard coded and should be set up as a variable. I’ve had a look at the other suggestions throughout the site but none of them cover this particular issue. So far, I’ve put together this configuration from various existing examples.

This is what I need.

User request ->
Redirect to https
HAProxy then makes a request to its backend pool using the URL http://<ip>:8080/site1/path
When the tomcat application returns links to assets they will have a path that will need to be redirected as well.

Site returns
If exists, remove the first site1 from the path.
I’ve gotten this far using examples and other guides from this site, but i’m at the limits of knowledge in this area. I’m not sure how to remove the site1 in the path. Any suggestions would be welcome.

# Frontend Definition
frontend tomcat_contexts
    bind *:80
    bind *:443 ssl crt /etc/haproxy/cert.pem
    acl http ssl_fc,not
    http-request redirect scheme https if http
    reqadd X-Forwarded-Proto:\ https
    default_backend cluster

# Backend Definition
backend cluster
    balance roundrobin
    cookie JSESSIONID prefix nocache

    # Perform Subdomain url rewrite
    http-request set-var(req.subdomain) req.hdr(host),lower,regsub(\.example\.com$,) if { hdr_end(host) -i }
    http-request set-path /%[var(req.subdomain)]%[path] if { var(req.subdomain) -m found }
    http-request set-header Host if { var(req.subdomain) -m found }

    # Cluster machines
    server app01 check cookie app01
    server app02 check cookie app02