Proper ProxyPassReverse with rewrite - apache

I'm trying to do something similar to the Coral cache.
For example if a client/browser looks up google.com.foo.com, it gets passed to a proxy by apache and serves a proxied/cached version of google.com to the client/browser.
My configuration for this so far is this:
ProxyRemote * http://localhost:8080
RewriteEngine on
RewriteCond %{http_host} ^(.*)\.foo\.com [NC]
RewriteRule ^(.*)$ http://%1$1 [P]
This actually works well as long as the website does not redirect to another site, but as soon as it redirects, it "bursts" out of the proxy and simply goes to the redirected site (oviously).
ProxyPassReverse should, as far as I understand it, prevent this, but I simply can't piece together how my ProxyPassReverse directive should look. After all, the hostname could be everything in this case...
Is this even possible?

Related

How to do this Apache mod-rewrite redirect transparently

I have this redirect (in Apache 2.4 VirtualHost *:80 configurations), which redirects example.com over to example.com/api/ (subfolder) and it works flawlessly.
Once I enter http://example.com into the browser, it takes me directly to http://example.com/api/.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /api$1 [R,L]
Now, what I need is for this to work transparently. I want http://example.com/api to load directly on the root domain: http://example.com
I've tried all kinds of suggestions I found on the internet, but nothing that would hide/mask the subfolder from the final URL that shows in the browser.
I figured there is an easier solution to this. I am running my app on a Tomcat server behind Apache and thus adding the following to Apache rules into VirtualHost config fixed the issue with subfolder transparency.
ProxyAddHeaders off
ProxyPass / http://server-host-for-example-com:8080/connect/
ProxyPassReverse / http://server-host-for-example-com:8080/connect/

redirect from subdomain to different host without 301

I need to point from someSub.somedomain.com to mysub.mydomain.com, and I need the url of the site to continue reading someSub.somedomain.com
I've tried many variations of:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
But I can't seem to get anything to work. Any advice?
This answer kind of already answers this question:
https://serverfault.com/questions/506623/masking-the-url-in-a-mod-rewrite
But it appears to me what you are looking for is a reverse proxy.
In the virtual host for your server:
ServerName somesub.somedomain.com
ProxyPass "/" http://mysub.mydomain.com/
ProxyPassReverse "/" http://mysub.somedomain.com/
Now when you go to your http://somesub.somedomain.com all the requests will behind the scenes actually be going to http://mysub.mydomain.com but the browser user won't see that.
There are various customizations to this you can make.
Don't forget to enable mod_proxy

Can't force 'https' and reverse proxy with Apache at the same time

Really racking my brain over this one. I need to always force 'https' whenever a user requests 'http' on my site, but at the same time I need to proxy pass from Apache to Tomcat (over http). I can't get these two pieces to work in tandem.
I have the https redirect defined here in httpd.conf:
<VirtualHost *:80> ServerName myserver.foo.com
Redirect / https://myserver.foo.com/
</VirtualHost>
Then for the proxy:
RewriteEngine on
RewriteLog /opt/HTTPServer/logs/rewrite_log
RewriteLogLevel 9
RewriteRule ^/testnew/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]
RewriteRule ^/testold/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=old [NC,P,QSA]
Note: This does actually work if I use ProxyPass instead, but I need to be able to add additional GET parameters to the request, hence the use of the [P] flag approach here.
So when I hit the following URL, I get the Apache HTTP Server page which reads "Not Found".
http://myserver.foo.com/testnew/MyApp/RunReport
In the access log, there's a 404
[10/Nov/2014:01:45:21 -0600] "GET /testnew/MyApp/RunReport HTTP/1.1" 404 321
Also, nothing gets written to the rewrite log.
As I understand it, RewriteRules will execute BEFORE Redirect, so even if the above URL did work (and I don't understand why it doesn't), it wouldn't get redirected from http to https. So how can I accomplish this?
I also tried this using only RewriteRules:
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301]
RewriteRule ^/testnew/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]
RewriteRule ^/testold/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=old [NC,P,QSA]
However with this, the URL gets translated to include the https scheme and hostname after the 1st redirect, so the subsequent RewriteRules fail to match. If I add the full 'https://myserver.foo.com' to the RewriteRule, it matches, but then the full URL gets translated back to http via the proxy.
I can't win! This seems to me like it would be a fairly common configuration. Did I completely miss something? I've been looking at this too long.
I was able to get this to work by moving the proxy RewriteRules under the *:443 VirtualHost and leaving the http -> https ones at the global level, i.e.
Listen 443
<VirtualHost *:443>
SSLEnable
SSLClientAuth None
RewriteEngine On
RewriteLog /opt/HTTPServer/logs/rewrite_log-443
RewriteLogLevel 9
RewriteRule ^/testnew/MyApp(.*)$ http://localhost:8080/test/MyApp$1?product=new [NC,P,QSA]
</VirtualHost>
...
...
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301]
Works beautifully now. :)

Apache rewrite or proxy with redirect of internal server

I am trying to use Apache as a proxy for an internal tomcat server, but the tomcat server performs a redirect that got me crazy
I first tried mod_proxy through:
ProxyPass / ajp://127.0.0.1:8045/bv-via-web/
ProxyPassReverse / ajp://127.0.0.1:8045/bv-via-web/
When accessing / (root) the internal server performs a redirect to /bv-via-web/hola which doesn't exist. It should redirect to /hola. How can I achieve this through the Apache config?
As a workaround I though I add the following:
ProxyPass /bv-via-web/ ajp://127.0.0.1:8045/bv-via-web/
ProxyPassReverse /bv-via-web/ ajp://127.0.0.1:8045/bv-via-web/
But that doesn't work as it will extend the url to an internal request of bv-via-web/bv-via-web/hola. So basically I want to change the url but don't know how that is possible with mod_proxy.
I then tried mod_rewrite, with the following:
RewriteEngine on
RewriteRule ^/bv-via-web/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P,L]
RewriteRule ^/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P,L]
But then when I open the root of the website it performs a redirect and http://127.0.0.1:8040/bv-via-web/hola appears in the browser address.
I don't understand, as it should work as an internal proxy due to the [P] flag.
What am I doing wrong ?
I solved it by adding:
ProxyPreserveHost On
Such it's not forwarded to the 127.0.0.1 as mentioned above.
So the total config snippet is:
RewriteEngine on
RewriteRule ^/bv-via-web/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P]
RewriteRule ^/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P]

Apache redirect after a rewrite

I'm trying to figure out how to properly do this. I'm hosting a domain that used to have a website also on the same server, however the website has now been moved to a different machine, but they want to keep the domain hosted on our DNS. Rather than changing the DNS record right now, I'm trying to figure out how to do a proxy redirect but I'm having some trouble.
Right now, I'm using the RewriteEngine to rewrite the URL as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.org [NC]
RewriteRule ^/(.*) http://www.domain.org/$1 [L,R]
This is in case someone looks up the website simply by http://domain.org it will get rewritten to http://www.domain.com - that works fine.
Now I need to redirect it to go to an IP address with a username:
http://111.222.333.444/~user
Rather than simply redirecting it to that address, I'd like to do a proxy where the domain will still be visible in the browser's address bar, while also keeping the above rule in place.
Suggestions anyone?
Make sure mod_proxy is enabled and do:
<VirtualHost *:80>
ServerName www.domain.com
ProxyPass / http://111.222.333.444/~user
</VirtualHost>