How do I connect my tomcat app to apache 2 so the paths aren't lame? - apache

I've got a tomcat instance with several apps running on it... I want the root of my new domain to go to one of these apps (context path of blah).. so I have the following set up:
<Location />
ProxyPass ajp://localhost:8025/blah
ProxyPassReverse ajp://localhost:8025/blah
</Location>
it kinda works... going to mydomain.com/index.jsp works except the app still thinks it needs to add the /blah/ to everything like css and js.. is there something I can do without deploying the app to ROOT or changing the tomcat server config? I'd like to keep all this kind of thing on the apache side, if it's possible.
I'm thinking I may not be understanding the proxypassreverse directive..

If you're wanting to server the app the /, Tomcat expects the app to be mounted at /, and have the name of ROOT. At least that's how I've always handled the situation personally. Even if you just symlink the app into ROOT, that should mitigate your problems. If you have an app placed in ${tomcat_home}/webapps/newapp, then Tomcat deploys it with a context of /newapp. At least, that's been the case in my history. Also, not sure if it matters but I've always used:
ProxyPass / ajp://localhost:8025/blah
ProxyPassReverse / ajp://localhost:8025/blah

it looks like this is kind of a pain in the rear.
apache is literally rewriting pages as it serves them...
I think I'll go a different route.

If you configure hosts on the Tomcat side as well then you can proxy to them and eliminate the context path for non-root webapps--in Tomcat server.xml:
<Host name="myhost">
<Context path="" docBase="/path/to/files" />
</Host>
And on the Apache side:
<VirtualHost *:80>
ServerName myhost
ProxyPass / ajp://myhost:8009/
ProxyPassReverse / ajp://myhost:8009/
</VirtualHost>
Hope that helps.

Related

Apache as front to Tomcat with proxy with mod_proxy_ajp on Mavericks Mac

I want to front Tomcat with Apache on a new Mac Server but Im missing some minor step. At the moment both servers are running, but port 8009 is not proxied correctly. 80 and 8080 is working.
I read that Mac/Apache has mod_proxy_ajp installed and should also be ready to run.
(Mentioned by for example Vladimir How to install mod_jk on Mac OS X)
Can someone guide me step by step on a new server from scratch, since all guides on internet, and documentations has not lead to success. (Sorry for asking, but im stuck after 1 week of nightly trials(errors). Im really going nuts here. I would appreciate folderpaths too, since files sometimes are located on more than one location)
Thanks in advance!
Regards
Niklas
Sorry for the delay, I was not allowed to answer my own question within 8 hrs (lack of points) but Yes I got it working! Mavericks Mac OSX server is really easy to setup and use with Apache as front to Tomcat. (If you know how to....)
Now I have tomcat integrated so that php with url: localhost is handeled by Apache and jsp url: localhost/examples -> runs tomcat but doesnt show the port, it looks integrated.
How I did it?
1) Uncomment if neccessary the line in server.xml for tomcat located in opt/apache-tomcat-8.0.9/conf:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Restart the tomcat server
2) Edit the Apache config file located: /Library/Server/Web/Config/apache2/httpd_server_app.conf
This strange name and location is caused by Mac Server.app and was what caused most problems, since I did not realize they renamed and copied the httpd.conf file to a second location! I was all the time changing the wrong config file...
I added this text with a texteditor TextMate (The program can show hidden files, and ask for adminpass when changing the file)
The Default webpage in Mac.Server is located as below Document Root
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Library/Server/Web/Data/Sites/Default"
<Proxy *>
AddDefaultCharset Off
Order deny,allow
Allow from localhost
</Proxy>
ProxyPass /examples ajp://localhost:8009/examples/
ProxyPassReverse /examples ajp://localhost:8009/examples/
</VirtualHost>
Please ensure your mod_proxy_ajp module is enable in Apache.
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
Once that is done put below code in your VirtualHost with relevant host details.
ProxyPreserveHost On
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

How to run PrimeFaces behind reverse proxy in a subdomain?

I have build an application with PrimeFaces and want to run that behind an apache reverse proxy.
My target url looks like this http://myserverurl.org:8080/myapplication/.
I want to access the application via subdomain like this http://myapplication.myserverurl.org.
I have configured a VirtualHost in apache:
<VirtualHost *:80>
ServerName myapplication.myserverurl.org
ProxyPass / http://myserverurl.org:8080/myapplication/
ProxyPassReverse / http://myserverurl.org:8080/myapplication/
</VirtualHost>
That works not so well. I can see the JSF page, but there is no CSS applied etc. I can see that the first request is redirected correctly, but the following requests (to load jQuery, CSS, etc.) are not.
They try to access an url like http://myapplication.myserverurl.org/myapplication/faces/javax.faces.resource/primefaces.js?ln=primefaces which is obviously wrong. They must not include the /myapplication/ path again, since the proxy redirects already to that path.
How can I solve this issue? Is this a PrimeFaces problem or a problem with my reverse proxy configuration?
Using (or not using) AJP has no bearing on resolving this specific issue.
Primefaces internally uses context path variable to include CSS and Javascript resources. Even using AJP you will end up with:
/unwantedAppContext/javax.faces.resource/jquery/jquery.js.jsf?ln=primefaces
What you could do to resolve this add another proxy pass to handle the unwanted context. This may not be the best solution, but it works.
<VirtualHost *:80>
ServerName myapplication.myserverurl.org
ProxyPass /myapplication/ http://myserverurl.org:8080/myapplication/
ProxyPassReverse /myapplication/ http://myserverurl.org:8080/myapplication/
ProxyPass / http://myserverurl.org:8080/myapplication/
ProxyPassReverse / http://myserverurl.org:8080/myapplication/
</VirtualHost>
The order of the pass matters.
This issue was also reported in the icefaces forum
http://www.icesoft.org/JForum/posts/list/4433.page#sthash.h1qSqiYg.dpbs
It may be different depending on the application server but, as a main rule, you should use AJP for the proxying.
First step is to enable ajp modules depending on OS. Ubuntu looks like this.
sudo a2enmod proxy proxy_ajp
Step 2, change the proxy definition in the apache conf to something like:
ProxyPass / ajp://localhost:8009/myapplication
ProxyPassReverse / ajp://localhost:8009/myapplication
Step 3 is to enable it on the application server. Again, it varies depending of the one you use. Tomcat has a commented out section in the server.xml. Glassfish has a check-box in the admin console and an asadmin command (but I can't remember it)
Consider using ProxyHTMLURLMap directive from mod_proxy_html module. This module manipulates output HTML links to be pointed to the right location. In your case all the links that tell http://myapplication.myserverurl.org/ need to be changed back to /, i.e.
ProxyHTMLURLMap http://myapplication.myserverurl.org/ /
This way you can modify any call-back links that are pointing to wrong location.
Answer of jjhavokk works but
for Graphics you need to reference them via request.contextPath
<img src="#{request.contextPath}/resources/yourfolder/yourpng.png" />
Place it in webapp/resources/yourfolder/yourpng.png
With my .css file this was not needed
h:outputStylesheet name="css/screen.css"
in webapp/resources/css

Tomcat Manager with mod_proxy and ROOT web application

Our application runs in Tomcat6 and we're proxying all requests from Apache to Tomcat via mod_proxy. that proxy config looks like:
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
<Location />
Order deny,allow
Allow from all
</Location>
Our application is also deployed as ROOT. I think that this is causing issues when attempting to get to the /manager URL but I'm not 100% sure and unclear on how I can verify this. Can anyone give me some pointers on how to resolve this? I'd like to use the manager app for remote deployments from jenkins.
That configuration will route all traffic to Tomcat, including requests to /manager. The way to confirm this is to look at Tomcat's access log that should show the same requests as httpd's access log.
Try http:// localhost/manager/html
It should work on both ports.

ProxyPassReverse to Tomcat adding path to URL

I'm running Railo 3 in Tomcat 6.0.32. The tomcat server is fronted by Apache 2.2.20. Tomcat and Apache are pre built binaries from openCSW. Railo is just the latest build war deployed in tomcat's autodeploy dir webapps.
Everything is working fine when I try to access railo and content on the tomcat server.
It fails however, when railo on tomcat redirects me to itself. Mostly, when a cfm script uses the CGI.script_name, it will be returned wrong.
On the Apache side, the content is available on www.hostname.com. Apache redirects the user to tomcat through AJP on www.hostname.com:8009/railo/content.
A script on tomcat (taken from open OAuth example) is available at:
/opt/csw/share/tomcat6/webapps/railo/content/oauth_test/examples/admin_consumers.cfm
When I access it and try to perform some action, it calls itself with a few parameters, but at that point, railo dumps out an error, complaining that the file can not be found:
Page /content/railo/content/oauth_test/examples/admin_consumers.cfm [/opt/csw/share/tomcat6/webapps/railo/content/railo/content/oauth_test/examples/admin_consumers.cfm] not found
As you can see railo added twice the relative path from tomcat: /railo/content/railo/content
This is my configuration for the virtual host in Apache:
<VirtualHost *:443>
ServerName www.hostname.com
DocumentRoot "/opt/www/hostname/htdocs/"
ProxyRequests Off
<proxy *="">
Order deny,allow
Allow from all
</proxy>
ProxyPass / ajp://www.hostname.com:8009/railo/content/
ProxyPassReverse / http://www.hostname.com:8888/railo/content/
</VirtualHost>
I tried several variant for the ProxyPassReverse directive, but with no luck so far. Based on extensive searches on the web (The Mystery of ProxyPassReverse), I tried this for the proxypassreverse:
ProxyPassReverse / ajp://www.hostname.com:8009/railo/content/
ProxyPassReverse / http://www.hostname.com:8888/railo/content/
ProxyPassReverse / http://localhost:8888/railo/content/
ProxyPassReverse / https://www.hostname.com
The tomcat server also has a virtual host defined like this:
<Host name="www.hostname.com">
<Context path="" docBase="/opt/csw/share/tomcat6/webapps/railo/content" />
</Host>
But everytime, I always get the error from Railo.
Has anyone ever seen this problem with Railo, or CGI, and has an idea how to fix it?
You are specifying "/railo/content" twice. Once in your "docBase" attribute and again in your Proxy attributes. So, requests being proxied through Apache are going to have "railo/content/" twice in their request paths because you have it listed twice: once in Apache, another time in Tomcat.
Try leaving off the /railo/content/ in your ProxyPassReverse attribute:
ProxyPassReverse / http://www.hostname.com:8888/
This will let the Tomcat config add the /railo/content/ bit all by itself.

jetty via apache mod_proxy

Using an Apache virtualhost and mod_proxy I want to access a java application (myapp) available in a jetty instance on port 8080.
With ProxyPass / localhost:8080/ on my apache virtualhost configuration I can access the application running in jetty with www.mydomain.com/myapp but I want the application to be accessed from www.mydomain.com.
Trying with ProxyPass / localhost:8080/myapp The application cannot be found because the request becomes www.mydomain.com/myappmyapp/.
Then tried with:
<Location />
ProxyPass localhost:8080/myapp/
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
</Location>
I can access the application but just for the first request. Subsequent requests become www.mydomain.com/myappmyapp/
After reading many times wiki.eclipse.org/Jetty/Tutorial/Apache and the apache mod_proxy docs the only way I managed to use the application properly from www.mydomain.com is with the following configuration:
<Location /myapp/>
ProxyPass localhost:8080/myapp/
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
</Location>
<Location />
ProxyPass localhost:8080/myapp/
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
</Location>
so the request is forwarded to the jetty application in both cases.
I am quite new to apache and jetty and I am pretty sure there is a better and more elegant way of achieving the same result.
In fact apache complains saying:
[warn] worker localhost:8080/myapp/ already used by another worker
The problem is that when you deploy your application in jetty with the context path /myapp, it will generate all links accordingly. Apache mod_proxy does all the rewriting at the HTTP level (headers) and won't do anything with the response body, keeping it as it is.
If you don't mind the /myapp sticking around, you can turn mod_rewrite on and include following two lines in your Location block:
RewriteEngine on
RewriteRule ^/myapp/(.*)$ /$1 [P]
If you would like to get rid of /myapp for good, then the only option left (assuming you don't want to waste CPU power on mod_proxy_html) is to configure virtual hosts, and deploy applications on virtual hosts with context path of /.
If you want your webapp to be accessible at the root of your site, what you need is to deploy the web application into the root of container. Usually, this is done by calling the war file ROOT.war instead of myapp.war (although this ultimately depend on the configuration of your Jetty deployer, which may be more complex than the default).
Yes it works from the jetty root, but I would like to have more than one application running. The configuration for myapp is under jetty's contexts folder:
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/mvc-showcase</Set>
<Set name="war"><SystemProperty name="jetty.home"/>/webapps/mvc-showcase.war</Set>
</Configure>
my jetty version is 6.1.22