HomePage » Tomcat

*SubTopics*


*Useful links*

Monitoring

http://www.lambdaprobe.org/d/download.htm

Connecting tomcat and apache using mod_jk or mod_proxy

One can connect tomcat to apache via mod_proxy or mod_jk.

Via mod_proxy_ajp on apache 2.2
Connecting tomcat and apache2.2 couldn't be easier. Apache2.2 has build-in support for AJP protocol. It's considerably easier than mod_jk. Here's a quick example on mounting the tomcat example webapps.

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

<IfModule proxy_module>
ProxyRequests Off
<Proxy *>
    Order allow,deny
    Allow from all
</Proxy>
ProxyPass /assets !
ProxyPass /css !
ProxyPass /scripts !
ProxyPass /js !
ProxyPass  /  http://localhost:8080/
ProxyPassReverse /  http://localhost:8080/

ProxyPass /examples ajp://localhost:8009/examples
ProxyPassReverse /examples ajp://localhost:8009/examples

# Or do it with rewrite
        RewriteEngine On
        RewriteCond %{REQUEST_URI} /.*\.jsp
        RewriteRule ^/(.*) ajp://localhost:8009/$1 [P]
        RewriteCond %{REQUEST_URI} /(servlet|examples).*
        RewriteRule ^/(.*) ajp://localhost:8009/$1 [P]

# Negate rewrite
    <Proxy balancer://internalBalancer stickysession=JSESSIONID>
                BalancerMember ajp://10.10.10.3:8509
                BalancerMember ajp://10.10.10.8:8509
        </Proxy>
    RewriteCond %{REQUEST_URI} !\.htm$
    RewriteRule /(.*) ajp://someBalancer/$1 [P]
   

# Enables maintenance page
RewriteEngine On
# Check for maintenance page
RewriteCond %{REQUEST_URI} !(maintenance.html$)
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteRule .* /maintenance.html [R,L]

# Forward everything to jboss
RewriteCond %{REQUEST_URI} !(maintenance.html$)
RewriteRule ^/(.*) ajp://localhost:8009/$1 [P]
</IfModule>


Load balancing example
<IfModule proxy_module>
ProxyRequests Off

# Defining a balancer
<Proxy balancer://tomcatfarm>
    Order allow,deny
    Allow from all
    BalancerMember ajp://localhost:8009/examples loadfactor=50
    BalancerMember ajp://192.168.13.10:8009/examples loadfactor=10
    # Member to use only when no other members are available
    BalancerMember ajp://10.0.0.1:8009/examples status=+H
    # Available methods byrequests, bytraffic
    ProxySet lbmethod=byrequests
</Proxy>

# Mapping /examples to your balancer
<Location /examples>
    SetEnv BALANCER_SESSION_STICKY jsessionid
    ProxyPass balancer://tomcatfarm/
    ProxyPassReverse balancer://tomcatfarm/
</Location>

# Enable balancer manager to show load statistics
<Location /balancer-manager>
    SetHandler balancer-manager
    Order allow,deny
    Allow from all
</Location>
</IfModule>


Balancer manager for the example above
image

Via tomcat-connector (mod_jk)
mod_jk has been around for some time. Installation is straight-forward on modern linux distribution.

Note: On Solaris, make, m4, libtool, automake and autoconf should be installed. Do a buildconf.sh before configure. Also export LD_LIBRARY_PATH="/usr/local/lib"

./configure --with-apxs=/usr/sbin/apxs
# Other options:
# --with-java-home=${JAVA_HOME} --with-java-platform=2 -enable-jni
make && make install


mod_jk setup
workers.properties
# Generic template - v1.0 - Ken Fong
# take out loadbalancer if talking to 1 tomcat.
workers.apache_log=/var/log/httpd/
workers.tomcat_home=/opt/tomcat
workers.java_home=/opt/java
ps=/

# Define list of workers that will be used
# for mapping requests
# When load-balancing is used, real workers should not be listed here.
worker.list=loadbalancer,status

# Definte default worker settings
worker.default.port=8009
worker.default.type=ajp13
worker.default.lbfactor=1
# no need to set socket_timeout in most case. default is 0
worker.default.socket_timeout=120
# for firewalled backend
worker.default.socket_keepalive=True
# ping listener before connect, timeout 5s
worker.default.ping_mode=P
worker.default.ping_timeout=5000
worker.default.connect_timeout=5000
# timeout between request and first response packet. set to longer in case of long running servlets
worker.default.reply_timeout=60000
# ping/ping before forwarding a request
# worker.prepost_timeout=5000
# worker.default.connection_pool_timeout=180

# Define Node1
worker.node1.reference=worker.default
worker.node1.host=192.168.17.101

# Define Node 2
worker.node2.reference=worker.default
worker.node2.host=192.168.17.102

# define load balancer
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=true
# Load balancing method can be [R]equest, [S]ession, [T]raffic, or [B]usyness
worker.loadbalancer.method=R
# How runtime data are sync-ed. [O]ptimistic or [P]essimistic
worker.loadbalancer.lock=O

# Status worker for managing load balancer
worker.status.type=status


httpd.conf
# Load mod_jk
#
LoadModule    jk_module  libexec/mod_jk.so
#AddModule     mod_jk.c

# Configure mod_jk
#
JkWorkersFile /usr/local/jakarta-tomcat/conf/workers.properties
JkLogFile     /usr/local/apache/logs/mod_jk.log
JkLogLevel    warn
# Set the following if you want all vhosts to inherhit JkMounts from global
JkMountCopy All

# Interesting option to try out. possibly eliminate the need to jkmount the same url twice
JkOptions +ForwardDirectories

# Sample mount point
JkMount /jmx-console ajp13
JkMount /jmx-console/* ajp13

# First Virtual Host.
#
<VirtualHost 10.0.0.1:80>
  DocumentRoot /web/host1
  ServerName host1.apache.org
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13
</VirtualHost>

# Second Virtual Host. Also accessible via HTTPS
#
<VirtualHost 10.0.0.2:80>
  DocumentRoot /web/host2
  ServerName host2.apache.org
  JkMountCopy On
</VirtualHost>

<VirtualHost 10.0.0.2:443>
  DocumentRoot /web/host2
  ServerName host2.apache.org
  SSLEngine On
  JkMountCopy On
</VirtualHost>


JkMount exclusion
Sometimes, you need to forward all requests to tomcat/jboss, except for maybe the images directory, which will be served out of apache. Here's how you can do it

JkMount /* worker1
SetEnvIf Request_URI "/images/*" no-jk


Then a fairly simply tomcat startup script
tomcat
#!/bin/sh                            
# tomcat Startup script for          
#                                    
# chkconfig: 2345 90 10              
# description: custom tomcat script to restart tomcat as the tomcat user
# @author                : Ken Fong                                    
# @origdate             : Wed Feb 27 11:22:32 EST 2008      
# @revision              : 0.2            

# Linux init functions
. /etc/rc.d/init.d/functions

export TOMCAT_HOME=/opt/tomcat        
export CATALINA_PID=$TOMCAT_HOME/logs/tomcat.pid
export JAVA_HOME=/usr                  
#export JAVA_OPTS="-Xmx512m"          
TOMCAT_USER=tomcat                  

RETVAL=0
LOCKFILE=/var/lock/subsys/tomcat

start() {
                echo -n "Starting tomcat..."
                chown -R $TOMCAT_USER:$TOMCAT_USER $TOMCAT_HOME
                ulimit -n 4096                                
                su $TOMCAT_USER -c $TOMCAT_HOME/bin/startup.sh
                # On BSD # su -m $TOMCAT_USER -c $TOMCAT_HOME/bin/startup.sh
                echo "Tomcat started with PID `cat $CATALINA_PID`"          
                RETVAL=$?                                                  
                echo                                                        
                [ $RETVAL = 0 ] && touch ${LOCKFILE}                        
                return $RETVAL
}

stop() {
                echo -n "Stopping tomcat..."
                su $TOMCAT_USER -c $TOMCAT_HOME/bin/shutdown.sh
                # on BSD # su -m $TOMCAT_USER -c $TOMCAT_HOME/bin/shutdown.sh
                #ps auxww | grep "$TOMCAT_HOME " | grep -v grep | awk '{print $2}' | xargs kill -9
                echo "Killing tomcat process in case normal shutdown was ineffective"
                if [ -n "`pidfileofproc $CATALINA_PID`" ] ; then
                    killproc $CATALINA_PID
                    # BSD # kill -9 `cat $CATALINA_PID`
                fi
                RETVAL=$?
                echo
                [ $RETVAL = 0 ] && rm -f ${LOCKFILE}
                return $RETVAL
}

case "$1" in
start)
                start
;;

stop)
                stop
;;

restart)
                stop
                sleep 10
                start
;;
esac
exit 0


Tomcat virtual host
If in some case you need to create virtual hosts with a dedicated webapps repository, add these to server.xml:
server.xml
<Host name="www.newdomain.com" appBase="/home/sites/newdomain.com"
        unpackWARs="true" autoDeploy="true"
        xmlValidation="false" xmlNamespaceAware="false">

       
        <Alias>newdomain.com</Alias>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="newdomain-" suffix=".log" pattern="common" resolveHosts="false"/>

</Host>

There are 1588 comments on this page. [Display comments]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki