route network traffic based on the subdirectory requested - apache

I have 3 back end nodes to handle traffic but round robin doesn't work in my case.
Is it possible to use Apache web server and route(proxy) all traffic from A-E to server 1, F-S to server 2 and T-Z to server 3? I don't want the url to change.
For example: build.amz.com/TEST should go to server 3 while build.amz.com/ECHO should go to server 1?
I am new to Apache, any directions towards how to achieve this is helpful

You can try something like this:
ProxyRequests Off
ProxyPreserveHost On
ProxyPassMatch ^/([A-Ka-k]+)(.*) http://server1/$1$2
ProxyPassReverse / http://server1/
ProxyPassMatch ^/([L-Pl-p]+)(.*) http://server2/$1$2
ProxyPassReverse / http://server2/
ProxyPassMatch ^/([Q-Zq-z]+)(.*) http://server3/$1$2
ProxyPassReverse / http://server3/
#ProxyPassMatch ^/(.*) http://serverdefault/$1
#ProxyPassReverse / http://serverdefault/

Related

Apache: Reverse Proxy based on subdomain

I'm trying to set a reverse proxy by replacing the domain and keeping the subdomain.
What I want to achieve is:
abc.example.org shall be reverse proxied to abc.hello.dev
xyz.example.org shall be reverse-proxied to xyz.hello.dev
Note: abc, xyz, etc. are completely dynamic.
What I'm currently not getting is the syntax for ProxyPass and ProxyPassReverse.
Thank you very much,
Cheers
Fritz
Found the solution:
<IfModule mod_proxy.c>
SetEnvIf Host "^([^.]*).example.org$" SUBDOMAIN=$1
ProxyPassInterpolateEnv On
ProxyPass / https://${SUBDOMAIN}.hello.dev/ interpolate
ProxyPassReverse / https://${SUBDOMAIN}.hello.dev/ interpolate
</IfModule>

Apache and Tomcat proxying

Recently, I was in need of using both Apache and Tomcat together in which Apache was to be used as the reverse proxy to forward requests to port 80 to localhost:8080 which I did like this:
<VirtualHost *:*>
ProxyPass / http://localhost:8080/app/
</VirtualHost>
And it works perfectly well.
Now, what I need to do is: I have Tomcat listening and serving on another port 8082. I need to be able to access it using www.mydomain.com:8082. I tried:
<VirtualHost *:8082>
ProxyPass / http://localhost:8082/app/
</VirtualHost>
But no luck. And I can't listen on 8082 because Tomcat is doing that.
What you have above is a (failed) attempt to map the / URL space into two different places. That's never going to work.
When proxying to Tomcat, it's never a good idea to rewrite URL paths (e.g. / -> /app/ because Tomcat is going to get all kinds of confused. It's much better to map individual applications:
<VirtualHost *:*>
ProxyPass /app1/ http://localhost:8080/app1/
ProxyPass /app2/ http://localhost:8080/app2/
ProxyPass /app3/ http://localhost:8082/app3/
ProxyPass /app4/ http://localhost:8082/app4/
# If you need a fall-back application for `/`, just map it last.
ProxyPass / http://localhost:8080/
</VirtualHost>
Note that the last line up there is mapping / to Tomcat's ROOT context (mounted on /'). Don't do this any other way, or you'll spend years trying to make everything work when you could have just done it the recommended way.

Apache ProxyPass to 2 different tomcat servers with same URI

I have 2 servers with apache http server (2.4.9) on them. I have 2 servers with tomcat 7.0.39 running on them. Ultimately I need to load balance between the 2, but right now am simply trying to get this ProxyPass configuration to work.
I have one url on the 2 apache servers- example.com. They are load balanced. The load balancing is in place and there is no issue.
I have 2 applications, 1 on each tomcat server. Each tomcat server has 2 instances of tomcat running- each on a different port. They are running a very similar application with the exact same uri.
I need to redirect:
example.com/site1 to example.com/app (for app1)
example.com/site2 to example.com/app (for app2)
I can redirect this easily enough for 1 application only:
ProxyPass /app/ AppSrv:8080/app/
ProxyPassReverse /app/ http://example.com/app/
I can get to the page, log in, no problem. For a singe application only.
When I set up a configuration for both applications, I can get to the login page of both applications as well when configured as such:
ProxyPass /site1/ AppSrv:8080/app
ProxyPassReserve /app/
ProxyPass /site2/ AppSrv:8081/app
ProxyPassReverse /app/
Both urls come up with the tomcat application login page. But neither work after this- they fail. The uri reads 'site1' (or 'site2') not 'app'. I cannot log in.
The application requires the uri '/app/' as the base uri in order to function.
The applications are listening on different ports, but have the same uri. How do I keep the uri in the url, but change it so the application responds?
Ultimately I have to balance this, but I've done that before. This is the part I'm having a hard time with.
Ok, I finally solved this. I had to modify the header.
ProxyPass /site1/ balancer://example.com/app/
ProxyPass /site2/ balancer://example.com/app/
<Location /site1>
ProxyPassReverse /app
ProxyPassReverseCookiePath /app /site1
Header edit Location ^(https?://)?example.com/app/ /site1/
</Location>
<Location /site2>
ProxyPassReverse /app
ProxyPassReverseCookiePath /app /site2
Header edit Location ^(https?://)?example.com/app/ /site2/
</Location>

Apache and multiple tomcats proxy

I have 1 apache server and two tomcat servers with two different applications. I want to use the apache as a proxy so that the user can access the application from the same url using different paths.
e.g.:
localhost/app1 --> localhost:8080/app1
localhost/app2 --> localhost:8181/app2
I tried all 3 mod proxy of apache (mod_jk, mod_proxy_http and mod_proxy_ajp) but the first application is working, whilst the second is not accessible.
This is the apache configuration I'm using:
ProxyPassMatch ^(/.*\.gif)$ !
ProxyPassMatch ^(/.*\.css)$ !
ProxyPassMatch ^(/.*\.png)$ !
ProxyPassMatch ^(/.*\.js)$ !
ProxyPassMatch ^(/.*\.jpeg)$ !
ProxyPassMatch ^(/.*\.jpg)$ !
ProxyRequests Off
ProxyPass /app1 ajp://localhost:8009/
ProxyPassReverse /app1 ajp://localhost:8009/
ProxyPass /app2 ajp://localhost:8909/
ProxyPassReverse /app2 ajp://localhost:8909/
With the above, I manage to view the tomcat root application using localhost/app1, but I
get "Service Temporarily Unavailable" (apache error) when accessing app2.
I need to keep the tomcat servers separate because I need to restart one of the applications often and it is not an option to save both apps on the same tomcat.
Can someone point me out what I'm doing wrong?
Thank you all.

Multiple ProxyPassReverse for a Virtual Host with identical URLs

We have a situation where we have one JBoss application that is being proxied by two Apache paths as a Virtual Host below:
<VirtualHost *:80>
ServerName localhost1
ProxyPass /abba/ http://localhost:8080/app/
ProxyPass /babba/ http://localhost:8080/app/
ProxyPassReverse /abba/ http://localhost:8080/app/
ProxyPassReverse /babba/ http://localhost:8080/app/
</VirtualHost>
The routing of /abba/ and /babba/ need to go to the same application - going forward we are using rewrites to add some parameters that the application uses to configure itself based on whether /abba/ or /babba/.
However when the application sends a redirect the ProxyPassReverse does not work as access sayfrom /babba/ gets redirected to /abba/.
I understand the reason as it's the same application - however is there are way of configuring Apache to support two difference routes (ProxyPass and ReverseProxyPass) to the same application.
Many Thanks
Have you tried duplicating the VirtualHost and changing the duplicate to server name "localhost2"?