Apache as proxy to use same URL with subroutes for different apps - apache

I am trying to set up an Apache2 web server as a proxy to redirect requests to different apps running on the server in separate Docker containers.
All requests going to route http://my_url.com/App2 should be directed to App2 running at localhost:8002.
All other requests to http://my_url.com should be redirected to App1 running on localhost:8001.
I used the following Apache configuration file:
VirtualHost my_url.com/:80>
ServerName my_url.com
ServerAlias www.my_url.com
ProxyPreserveHost On
ProxyPass /App2/ http://localhost:8002/
ProxyPassReverse /App2/ http://localhost:8002/
ProxyPass / http://localhost:8001/
ProxyPassReverse / http://localhost:8001/
</VirtualHost>
If I try to access App2, it initially redirects to the correct Docker container. However, the Problem is now that if App2 does a redirect to for example the /login route, the subroute /App2/ gets lost and Apache tries to find /login in App1 container.
What should happen is:
App2 wants to redirect to /login and makes the browser access my_url.com/App2/login and not my_url.com/login.
Is this achievable with just Apache configurations or do I need to change the redirects in App2 Docker container?

The issue was the line ProxyPreserveHost On. This resulted in Apache adding the header field:
X-Forwarded-Host: 'my_url.com'
for every request.
Thats why the ProxyPassReverse:
ProxyPassReverse /App2/ http://localhost:8002/
didn't work since it is only rewriting requests from http://localhost:8002/.
Setting ProxyPreserveHost Off (which is also the default) solved the issue for me.

Related

Apache Reverse Proxy: (Keeping Existing route alive)

I have an app running on localhost:3000. I also have another app that is configured to run on virtualhost local.testserver.com
I want to create an apache reverse proxy that will route all request from local.testserver.com/finance to my app that runs at localhost:3000.
At the same time, I want to be able to access my app when I go to localhost:3000.
Below is the configuration I am using currently. This works for routing request from local.testserver.com/finance to my localhost:3000 app. But whenever I visit localhost:3000 directly it redirects me back to local.testserver.com
<Location /finance >
ProxyPass http://localhost:3000
ProxyPassReverse http://localhost:3000
</Location>
You will need to add the path on the ProxyPassReverse setting to "/finance" so it knows where to reverse requests back.
You may not need the <Location /finance>...</Location> part
ProxyPass "/finance" http://localhost:3000
ProxyPassReverse "/finance" "http://localhost:3000"
Please refer to the documentation to better understand how
ProxyPassReverse works here:
https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypassreverse

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"?

Redirect URL path to forward to tomcat servlet using Apache/mod_proxy

I currently have a tomcat servlet 1 running under the ROOT:
api1.myhost.com:8080/
I'm using mod_proxy and simply forwarding all requests from
api1.myhost.com to this instance. This is working as of today.
I now have installed a second servlet 2 which runs under the same instance of tomcat (same IP address):
www.myhost.com:8080/servlet2
I want all requests to a new URL api2 to go to that second servlet such that:
api2.myhost.com
now gets forwarded to the second servlet instance.
I've created an A record such that api2.myhost.com points to my server IP. How do you make api2.myhost.com forward to www.myhost.com:8080/servlet2 ?
You need to make two VirtualHost's with on pointing to the first webapp, the other to the second.
<VirtualHost *:80>
ServerName api1.myhost.com
ProxyPass / http://api1.myhost.com:8080/
ProxyPassReverse / http://api1.myhost.com:8080/
</VirtualHost>
<VirtualHost *:80>
ServerName api2.myhost.com
ProxyPass / http://www.myhost.com:8080/servlet2
ProxyPassReverse / http://www.myhost.com:8080/servlet2
</VirtualHost>
Note that since the path will be different on tomcat than on apache, you will need to use relative URLs in your application.