Apache and Tomcat proxying - apache

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.

Related

How do I setup an Apache ProxyPass / Reverse Proxy while listening on an alternate port?

I had Apache configuration which was using a ProxyPass/ProxyPassReverse with the following example syntax:
ProxyPass /myprog http://localhost:8080/myprog
ProxyPassReverse /myprog http://localhost:8080/myprog
Then, I moved Apache from port 80, to 8880, and put another web server on port 80. I can access some things correctly now on port 8880 - those files which are hosted directly on Apache. But my proxy pass (to Tomcat) now fails when I try to access it at: http://some.domain:8880/myprog.
How do I correct the ProxyPass/Reseverse to account for the port change? (I assumed, perhaps natively, the port spec was implicit...).
You need to put your configuration on a VirtualHost tag.
<VirtualHost *:8880>
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /myprog http://localhost:8080/myprog
ProxyPassReverse /myprog http://localhost:8080/myprog
</VirtualHost>

Running multiple root\no context web apps on single host

I have a host with a single web app and at the moment I am accessing it via www.hostnameA.com/ as the web app is deployed to tomcat/webapps as the tomcat ROOT web app.
Now I need to add another web app to my host and I also want this one to have no context either but will access it via another hostname www.hostnameB.com/ but I can only deploy one ROOT tomcat web app.
I should have added that I am using apache as well and my virtual host looks like:
<VirtualHost *:80>
ServerName www.hostnameA.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
I tried renaming the war file to webAppA and then appending that to the proxypass but it gave me a 400 error and kept appending /webAppA to the URL:
ProxyPass / http://localhost:8080/webAppA
Is there a solution to this? I dont really want to run multiple instances of tomcat on different ports just for this, is there another option?
As discussed above, most straightforward solution would be to use Apache's mod_proxy_ajp, allowing proxying and AJP forwarding at the same time. Configuration should look something like:
<VirtualHost *:80>
ServerName www.hostnameA.com
ProxyPass / ajp://localhost:8009/webAppA/
ProxyPassReverse / http://www.hostnameA.com/webAppA
[...]
...and same with B for www.hostnameB.com.

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.

What are my options to deploy Go applications alongside PHP applications?

What I'm basically trying to accomplish is having my main website running a CMS written in Go. This will be located at www.example.com.
I also have applications written in PHP located in directories, such as www.example.com/clients/
How can I serve example.com/clients using Apache/PHP while serving example.com using Go built-in web server?
Via mod_proxy in Apache2, you can proxy different paths into different destinations at localhost or anywhere else accessible by your server, including within your local network (if your server can access it).
For this you would use ProxyPass (Apache2 Docs for ProxyPass, which is very useful reading) like the example below:
<VirtualHost *:80>
ServerName some.example.host.xyz
DocumentRoot /var/www/your-document-root
Alias /clients/ /var/www/clients/
ProxyPass /clients/ !
ScriptAlias /something-using-cgi/ /var/www/cgi-stuff/
ProxyPass /something-using-cgi/ !
ProxyPreserveHost On
ProxyPass / http://localhost:9876/
ProxyPassReverse / http://localhost:9876/
ProxyPass /elsewhere/ http://elsewhere.example.host.xyz:1234/
ProxyPassReverse /elsewhere/ http://elsewhere.example.host.xyz:1234/
</VirtualHost>
You'll want to be sure that you set your proxy security such that external users can't use your reverse proxy as a forward proxy, too. You can do that via ProxyRequests as described in the official Apache2 docs. The way I did this on a server is to put this in your server-wide config (you should verify on your own that this is secure enough):
# disables forward proxy
ProxyRequests Off
Andrew Gerrand has a good blog post about this for nginx but the principle is the same for Apache.
You want to set up Apache as a reverse proxy for requests coming in for the Go application.
For Apache you want to look at mod_proxy

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