Forward connection from Apache on 80 to Tomcat 6 on 8080 with mod_proxy

11,203

If there is no specific reason to use mod_proxy to deploy your applications then Tomcat Connectors / mod_jk is a more suitable solution.

Please refer to

Tomcat connectors howto

for info how to setup connectors on Apache side. Notice that JkMount /examples/* worker1 mounts worker by context (so that everything in examples is going to be handled by Tomcat), in your configuration you would want to mount by extension (or even just *), something like this:

JkMount  /*.jsp worker1
JkMount  /*.do worker1
JkMount  /*.whatever worker1

so that everything matching *.jsp *.do and *.whatever would be handled by Tomcat, and the rest served by apache. Note: JkMount lines need to be inside your definition (in some cases).

mod_jk doesn't use HTTP to connect to Tomcat, so you can disable the HTTP connector (the one that listens to 8080 by default). You will need to enable AJP connector (not sure if it's enabled by default) with a line in server.xml something like this one:

<Connector port="8009"
            enableLookups="false" redirectPort="8443" 
            protocol="AJP/1.3"/>

For Tomcat to recognize which application should respond to the calls you would need to do Host configuration and define a Host with a name matching you site name.

Then you define an apache virtual host with DocumentRoot matching the docBase of Tomcat's application and ServerName matching the Tomcat's Host name, like this:

httpd.conf:

<VirtualHost *:80>
DocumentRoot /var/www/yoursite.com
ServerName www.yoursite.com
</VirtualHost>

server.xml:

<Host name="www.yoursite.com" appBase="/var/www"
   unpackWARs="false" autoDeploy="false" deployOnStartup="false"
   xmlValidation="false" xmlNamespaceAware="false">
   <Context path="" docBase="/var/www/yoursite.com"/>

Please note that this setup is not for a WAR file deployment, but rather for an unpacked (exploded, extracted) war file. WAR is a ZIP archive in case you didn't know.

I apologize for making it a bit vague, but it works, and this is the right way to deploy Tomcat applications behind Apache HTTPD.

If you google for it you will find manuals with walkthoughs, this one seems to be a good one.

Share:
11,203
piojo
Author by

piojo

Updated on October 03, 2022

Comments

  • piojo
    piojo over 1 year

    I've a domain www.example.com, now I'm trying to forward the browser connection to http://myserveripaddress:8080/MyAppName, in order to let the user surf MyApp when they type www.example.com on their browser...

    To do this I wrote this tag within httpd.conf file of Apache (2.2.20):

    <VirtualHost *:80>
        ServerName www.example.com
        ProxyRequest Off
    <Proxy *>
    Order deny,allow
    Allow from all
    </Proxy>
    ProxyPass / http://myserveripaddress:8080/MyAppName/
    ProxyPassReverse / http://myserveripaddress:8080/MyAppName/    
    <Location />
    Order allow,deny
    Allow from all
    </Location>
    </VirtualHost>
    

    Then I've used the following tag into server.xml file of tomcat:

    ... ...       

    The problem is that when I type www.example.com on browser I've this answer You don't have permission to access / on this server. Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2 Server at www.example.com Port 80

    If I try to connect to www.example.com/MyAppName, it works....

    So finally, how can i connect to MyApp just typing www.example.com into web browser??


    Updated at 21.41 CET 1/1/11

    This is my configuration:

    httpd.conf

    <VirtualHost *:80>
        DocumentRoot /var/www/MyAppName
        ServerName www.example.com
    </VirtualHost>
    
    
    
    
    <IfModule mod_jk.c>
       JKWorkersFile /etc/apache2/workers.properties
       JkShmFile /var/log/apache2/mod_jk.shm
       JKLogFile /var/log/apache2/mod_jk.log
       JkLogLevel debug
       JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
       JkMount /MyAppName/* worker1
    </IfModule> 
    

    I also tried:

    JkMount /* worker1
    

    Within server.xml

    <Connector port="8009" protocol="AJP/1.3" />
    
    
    <Host name="www.example.com"  appBase="/var/www/"
           unpackWARs="true" autoDeploy="true" deployOnStartup="true"
           xmlValidation="false" xmlNamespaceAware="false">
           <Context path="" docBase="/var/www/MyAppName"/>
    

    Within worker.properties

    worker.list=worker1
    worker.worker1.type=ajp13
    worker.worker1.host=localhost
    worker.worker1.port=8009
    

    My configuration is Apache 2.2.20, Tomcat 6, running on Mac Mini Server OS X Lion....

    If i try to access www.example.com, I correctly se my index.html page, but when I click on a button which is linked to another page or a Servlet or a JSP in my Project, this is the message:

    404 Not Found Error
    Not Found
    
    The requested URL /MyAppName/RegistrationPage was not found on this server.
    
    Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2 mod_jk/1.2.30 Server at www.example.com Port 80
    

    Moreover if a JSP is directly within MyAppName page (not within a subdirectory), it prints the code, the java engine doesn't interpret the code.

  • piojo
    piojo over 12 years
    so if I want Tomcat to handle .jsp file and .html, i need to write within httpd.conf: JkMount /*.jsp worker1 JkMount /*.html worker1
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    yes, I haven't heard of any multiple patterns per line syntax, unfortunately
  • piojo
    piojo over 12 years
    Then in the server.xml file of tomcat i write <Host name="www.example.com", and appBase ??? -> "webApps/MyAppName" ?
  • piojo
    piojo over 12 years
    Sorry... how about the apache virtual host? do i need write it within httpd.conf ?
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    appBase="/var/www/yoursite.com", virtual host example will put in the answer
  • piojo
    piojo over 12 years
    sorry but I have no www/yoursite.com directory, I just want to www.example.com connection go to MyAppName deployed under Tomcat...I've the war file...
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    set unpackWARs="true" and you will magically start to have www/yoursite.com directory after the first deployment
  • piojo
    piojo over 12 years
    I got this error on Tomcat log: GRAVE: Error starting static Resources java.lang.IllegalArgumentException: Document base /var/www/example.com does not exist or is not a readable directory
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    the example code was was the unpacked application... you can put yoursite.war to /var/www and set docBase="/var/www/yoursite.com.war", then after the application get unpacked you can switch back
  • piojo
    piojo over 12 years
    So the previous tag become - DocumentRoot /var/www/MyAppName.war - <Context path="" docBase="/var/www/MyAppName.war"/> is it rihgt? Moreover, do i need to leave unpackWar, autoDeploy, deployOnStartup, xmlValidation, xmlNamespaceAware all false?
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    Ok, to make things simple just unpack the war file to the folder and go with the setup from th answer.
  • piojo
    piojo over 12 years
    It just works with index.html, but if I try to visit another page or a JSP page either it doesn't find the page (because the Java Class are within the web INF) or (for JSP page) it does not read the Java Server Page in fact he write the code, instead of intepret it...
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    this means that Worker didn't work for some reason. If you re-enable http listener on port 8080 and go to www.yousite.com/some.jsp - will it work fine?
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    i meant www.yousite.com:8080/some.jsp
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    @ValerioViglione also please check what jk log is saying, you define it with JkLogFile and JkLogLevel directives
  • piojo
    piojo over 12 years
    I'v just checked the jk log but in info level I've neither error nor warning, in debug it send me a lot of line... how can I send them to you... Can you add me on Facebook facebook.com/valerio.viglione
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    just paste them to pastebin.com
  • piojo
    piojo over 12 years
    pastebin.com/bD3iZQBp , maybe at line 37, when it says worker myWorker contact is 'localhost:8009', instead of MyServerStaticIpAddress
  • piojo
    piojo over 12 years
    I've just try to change worker.myworker.host=myserverstaticipaddress, but it doesn't work, when i click on a index.htm butt i reach: The requested URL /MyAppName/RegistrationPage was not found on this server. Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2 mod_jk/1.2.30 Server at www.example.com Port 80 I've a Mac Mini Server with Lion os and Tomcat 6
  • Oleg Mikheev
    Oleg Mikheev over 12 years
    if you have a default installation of Tomcat and Apache HTTPD then following this quickguide will work, maybe you could start with that and then see what's different in your particular case...
  • piojo
    piojo over 12 years
    Hi Oleg... I try to reset all and start again...but It desn't work...