Apache split wildcard sub-domain for ProxyPass - apache

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/
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>

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/

Related

How to Redirection and apply TLS with single web server multiple ports using RewriteEngine and ProxyPassReverse in Apache configuration

I'm running into a very serious problem.
Hope someone kind can help.
Please see the code below first.
This is part of my Apache configuration.
First, the URL of the target web server is http://localhost:30001 ~ 30010. Identifies the site to be accessed through each PORT.
When I receive input as "http://localhost/wksp/30001", I use RewriteEngine to make it "http://localhost:30001". This part works without any problems.
However, I tried to apply TLS and run "https" through ProxyPass and ProxyPassReverse again, but it is very difficult.
From what I understand, ProxyPassReverse accesses "http://localhost:[PORT]/" at first, but then communicates with Proxy through "https://localhost/".
Therefore, when communicating with the https protocol, the PORT cannot be dynamically acquired. For example, if http://localhost:30001 is the target, you need to communicate with the proxy server through port 30001.
In my code now, when HTTPS=on, PORT is written as 30001, but what I want is to keep holding the variable $1 when HTTPS=off.
If you have another approach, that's fine. lots of advice Thank.
<VirtualHost *:443>
...
SSLEngine On
ProxyPreserveHost On
SSLProxyEngine On
ProxyRequests Off
RequestHeader set Front-End-Https "On"
...
RewriteEngine On
RewriteRule ^/wksp/(.*) http://localhost:$1 [P]
ProxyPassReverse / http://localhost:$1
<Location />
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond "%{HTTPS}" =off
RewriteRule ^:(.*) http://localhost:$1/ [P]
ProxyPassReverse http://localhost:$1/
RewriteCond "%{HTTPS}" =on
ProxyPass http://localhost:30001/
ProxyPassReverse http://localhost:30001/
</Location>
</VirtualHost>
If HTTPS=on, if you change the url port of ProxyPass and ProxyPassReverse to $1, the following error is displayed.
httpd[1584880]: AH00526: Syntax error on line 55 of /etc/httpd/conf.d/wksp-tls.conf:
ProxyPass Unable to parse URL: http://localhost:$1/

Apache ProxyPass only for subdomain and folder

I want to use ProxyPass and only apply for the following subdomain and folder:
de.domain.com/blog/ -> http://blog.olddomain.com/de/
Our new page will have multiple subdomains (each per locale). When i only use this:
ProxyPass /blog/ http://blog.olddomain.com/de/
ProxyPassReverse /blog/ http://blog.olddomain.com/de/
All subdomains are using the rule. So my question is, how can i tell ProxyPass only apply this on de.domain.com/blog/ and not on en.domain.com/blog/ etc.
Thank you!
The first option would be to split the VirtualHost configuration, and first have separate virtual host for de.domain.com which will include the above ProxyPass directives, and then have another virtual host for *.domain.com, without ProxyPass:
<VirtualHost *:443>
ServerName de.example.com
ProxyPass /blog/ http://blog.olddomain.com/de/
ProxyPassReverse /blog/ http://blog.olddomain.com/de/
# Other directives here
</VirtualHost>
<VirtualHost *:443>
ServerName *.example.com
# Other directives here
</VirtualHost>
If you don't want to do that (for example because it might make configuration maintenance more complicated) you can try:
RewriteEngine On
RewriteCond %{HTTP_HOST} =de.example.com [NC]
RewriteRule /blog/(.*) http://blog.olddomain.com/de/$1 [NC,P,L]
ProxyPassReverse /blog/ http://blog.olddomain.com/de/

Apache proxy pass all urls and rewrite only one specific url

I have a website running the Ghost Blog engine in the back end. I configured the subdomain blog.domain.com to proxy to ghost engine (localhost:2368) but I need to verify that subdomain in google search console so I need the blog.domain.com/googlefile.html to return a specific string (that same string is available at domain.com/googlefile.html). How do I do that?
My virtual host config:
ServerName blog.example.com
ServerAlias *.blog.example.com
#here is what I tried
#RewriteEngine On
#RewriteCond %{HTTP_HOST} blog\.example\.com
#RewriteRule googlefile.html https://example.com/googlefile.html
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http://127.0.0.1:2368/
Btw. all domains are https.
The solution was to enable SSLProxyEngine so I can proxy https urls and also use mod_rewrite with proxy ignore url
SSLProxyEngine On # enable SSLProxyEngine
ServerName blog.example.com
ServerAlias *.blog.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} blog\.example\.com
RewriteRule googlefile.html https://example.com/googlefile.html [P]
ProxyPreserveHost On
ProxyPass googlefile.html ! # ignore the rewrited url
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http://127.0.0.1:2368/

How to forward requests to another URL?

Currently, I have the following rule in my httpd.conf file to forward all requests from port 80 to port 8080 to be served by GlassFish app server:
<VirtualHost *:80>
ServerAdmin admin#myserver.com
ServerName myserver.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
Now, I need to add a rule such that all requests to http://myserver.com/ will be forwarded to http://myserver.com/page/index.html and the URL should still appear to be http://myserver.com/ on the client's browser. I tried to add the following rules inside the above VirtualHost:
RewriteEngine On
RewriteRule http://myserver.com/ http://myserver.com/page/index.html
or
RewriteEngine On
RewriteRule ^/ http://myserver.com/page/index.html
or
RewriteEngine On
RewriteRule ^/index.html http://myserver.com/page/index.html
However, when I go to http://myserver.com/, the browser have this error: This webpage has a redirect loop. The 3rd rule can only work if I go to http://myserver.com/index.html.
I am a total noob at writing rules for Apache. Hence, I'd be very grateful if you could show me what I've done wrong here :).
UPDATE:
The following rule works perfectly:
RewriteEngine On
RewriteRule ^/$ /page/index.html [R]
You need to add a $ indicating the end of the URI:
RewriteEngine On
RewriteRule ^/$ http://myserver.com/page/index.html
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
Without the $, the regex ^/ matches /page/index.html which will cause it to redirect again, and it'll match again, and redirect again, etc.

hosting multiple websites on apache under one ip address

I am trying to host multiple websites (app.diff1.com and app.diff2.com running on tomcat server) on my apache server which are running on ports 8082 and 8083 respectively, to access them i want to use a single domain name (app.in.xxx.com/diff) along with a differentiater (diff1/diff2).I used url rewriting to change the domain names respectively. Now when i am trying to access the websites i always end up with hitting the first virtual host.
Please suggest me if i am not approaching the solution correctly.
Code snippet:
in httpd.conf file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app\.in\.xxx\.com/([a-z]*)$
RewriteRule ^ http://www.app.%1.com [L,R=301]
</IfModule>
<IfModule mod_proxy.c>
ProxyRequests off
</IfModule>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.app.diff1.com
ProxyPass / http://www.app.diff1.com:8082/
ProxyPassReverse / http://www.app.diff1.com:8082/
</VirtualHost>
<VirtualHost *:80>
ServerName www.app.diff2.com
ProxyPass / http://www.app.diff2.com:8083/
ProxyPassReverse / http://www.app.diff2.com:8083/
</VirtualHost>
Move your rewrites into the first listed virtual host, and stop trying to capture the first path component of the URL as if it were part of HTTP_HOST -- capture it in the RewriteRule itself and use $1.
Also see ServerPath which is an esoteric way to do the same mapping.