Search code examples
apachemod-rewritehttpd.confmod-proxy

Apache split wildcard sub-domain for ProxyPass


I have Apache config where I would need to split the http host domain which includes dashed subdomain and build a new path using these 3 of these matching groups in proxy pass or rewrite rule.

Example urls:

  • kube-test-selfservice.example.com/app/
  • kube-staging-selfservice.example.com/app2/

Would need to proxied to:

  • balancer://kubernetes/test/selfservice/app/
  • balancer://kubernetes/staging/selfservice/app2/

desired proxy

It is important that the test and selfservice in this example are captured as these values change. kube can be hardcoded for distinguishing this host under.

I currently only have basic proxy setup, have tried multiple regex rewrites, but as I am not very familiar with apache, would like some advice on that part.

<VirtualHost *:443>
    ServerName example.com
    ServerAlias *.example.com

    ProxyRequests Off
    ProxyPreserveHost On
    AddDefaultCharset Off

    <Proxy "balancer://kubernetes">
       BalancerMember http://192.168.1.244:30001 route=node1 timeout=600
    </Proxy>

    ProxyPass / "balancer://kubernetes/"
    ProxyPassReverse / "balancer://kubernetes/"

</VirtualHost>

Solution

  • Please try this, i try to run below and it worked :

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^kube-([a-z0-9_]+.)?-([a-z0-9_]+.)?.example.com [NC]  
    RewriteRule "^/?(.*)"  http://kubernetes/%1/%2%{REQUEST_URI} [R=301,L]
    

    Used URL :

    http://kube-test-selfservice.example.com/app/

    URL Rewritten to :

    http://kubernetes/test/selfservice/app/