Apache to tomcat proxy is not working - apache

I have got apache 2.4, and 1 tomcat (connector port - 8081 and AJP - 8009) server (both on same linux box) on which I have an application folder named 'MyApp' in webapps directory.
Tomcat direct URL is - http://localhost:8081/MyApp/MyApp --> This links loads fine and displays the images, jquery and js works fine.
I am proxying all the requests to tomcat from apache using mod_proxy as mentioned below -
ProxyPass /MyApp ajp://localhost:8009/MyApp/MyApp
ProxyPassReverse /MyApp ajp://localhost:8009/MyApp/MyApp
Now, when I try to access it through web server URL, the URL loads but the images, css, jquery, which are inside the /u01/tomcat/webapps/MyApp directory doesnt work.
If I try to load the direct URL of the image, for example - http://localhost/MyApp/images/logo.jpg it doesnt work,
In the body of the image, I see -
HTTP Status 404 - /MyApp/MyApp/images/incidentReport.jpg
See above line, it is adding one more ''MyApp' to fetch the image
On the other hand, the tomcat URL [ localhost:8081/MyApp/images/logo.jpg ], loads fine.
What could be the issue? The developer of the application has designed it in such a way that it should work with a double 'MyApp' i.e. localhost:8081/MyApp/MyApp
What else do I need to do either on webserver/tomcat in order to make this work?

You need to follow these steps:
Step 1: Before configuring Apache, you should enable the necessary modules.
a2enmod proxy
a2enmod proxy_http
Step 2: Next, you are going to modify the default configuration file 000-default.conf inside /etc/apache2/sites-enabled to set up "proxying" functionality.
<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://0.0.0.0:8081/
ProxyPassReverse / http://0.0.0.0:8081/
ServerName localhost
</VirtualHost>
Step 3: Next, you are going to modify the server.xml file.
<Host name="www.drew-jocham.com" appbase="webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="resumesite_log." suffix=".txt"
pattern="common"/>
<Context path="" docBase="/MyApp/MyApp" debug="0" reloadable="true"/>
</Host>
Once you are done with your configuration, you will need to restart the cloud server for the changes to go into effect. Execute the following command to restart Apache: service apache2 restart
And that’s it!
Read more: https://medium.com/#ldclakmal/deploy-a-java-web-application-in-digitalocean-882226dcdbd5

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/

After using mod_proxy the web application is no more accessible from other computer

I used apache proxy support in tomcat to remove the port number from url. Here is what I did,
In http.conf file I added
LoadModule proxy_module mod_proxy.so
ProxyPass /alfresco http://localhost:8080/alfresco
ProxyPassReverse /alfresco http://localhost:8080/alfresco
In server.xml file of tomcat
<Connector port="8080" ...
proxyPort="80"/> (I didn't give proxy name)
Now I am able to access the web application using http:\\localhost/alfresco in my machine. But when I try this from other machine in my lan using http:\\machine1\alfresco I am getting page cannot be displayed error. But if I try http:\\machine1 I am getting It works page of apache. What went wrong?
Previously I am able to use the application from other machine.
I removed the proxy port="80" in server.xml file and now its working

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.

TeamCity WebServer with Apache Proxy get "Method GET not implemented (try POST)"

I am trying setup TeamCity webserver on a server run Apache Proxy.
I added Add this to my Apache conf file
ProxyPass /TeamCity http://localhost/TeamCity
ProxyPassReverse /TeamCity http://localhost/TeamCity
And
added the Context to my TeamCity conf server.xml file in the Host section
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
docBase="C:\TeamCity\webapps\ROOT"
debug="1"
reloadable="true" > </Context>
Anyone know why I still get ""Method GET not implemented (try POST)"?
What is that actually mean?
I'm not sure how this is going to work.. It looks like you're trying to proxy http://your-server/TeamCity to http://localhost/TeamCity, unless your apache is on a different port..?
I've just implemented the a ProxyPass for my TeamCity install. My <Context> looks like:
<Host ...>
<Context path="/build" docBase="../webapps/ROOT"></Context>
</Host>
..Giving TeamCity the root URL of http://localhost:8111/build - See here for the apache doco on the tag. Without this, TC's Tomcat would redirect you to http://your-server/login.html instead of /build/login.html (results in a 404).
My httpd.conf contains (in the mod-proxy section):
ProxyPass /build http://localhost:8111/build
ProxyPassReverse /build http://localhost:8111/build
Which will proxy the requests and responses through /build to the new TeamCity URL. Works for me!

Configuration to get the images from the apache

I have multiple Tomcat servers. In each tomcat I am loading all images. I want to move the images from Tomcat to Apache and access the images from there.
My question is: is there any configuration to get the images in a Java web application from Apache instead of Tomcat?
I don't think so - images are shown by resolving their URL, and in most cases it is relative to the current page.
But you can use mod_proxy (or mod_jk) to use all your tomcats through Apache.
Hide your tomcat servers behind an apache server and then you can do something like this:
<VirtualHost www.example.com:80>
ServerName www.example.com
DocumentRoot /var/www/html
ProxyPass /img !
ProxyPass / ajp://localhost:1234/
</VirtualHost>
Apache will serve /img from /var/www/html/img and other requests will be sent to tomcat. This configuration needs mod_proxy_ajp apache module to be active. Tomcat must listen to AJP requests on the given port, use:
<Connector port="1234" protocol="AJP/1.3" redirectPort="8443" address="127.0.0.1" URIEncoding="UTF-8" />
And configure tomcat to serve www.example.com or the default virtual host.
<Host
name="www.example.com"
appBase="/path/to/tomcat/apps/www.example.com"
unpackWARs="true"
autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false"
/>
or
<Host
name="localhost"
appBase="/path/to/tomcat/apps/www.example.com"
unpackWARs="true"
autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false"
/>
Deploy your webapp to /path/to/tomcat/apps/www.example.com/ as ROOT.war, this should be enough to have the whole setup up and running.