Apache2.2 / Tomcat6 / mod_jk / Suse 11.1

Dear all,

I’m trying to integrate tomcat6 with apache 2.2 using mod_jk. Somehow it seems that the requests to apache are not properly forwarded to tomcat: If I want to activate a jsp through apache, I get to see the source code for the jsp. If I access the same jsp directly through tomcat, (using port 8080) it works fine.

I

  • loaded the jk module in apache
  • created a workers,property file in the tomcat configuration directory $CATALINA_HOME/conf. See below.
  • created a jk.conf file in /etc/apache2/conf.d/jk.conf . See below.

My webapps are in /srv/tomcat6/webapps . As a test case, I’m trying to access the jsp/servlet examples that come with tomcat through apache.

Below are my configuration files. I have the following workers.properties file:


worker.list=worker1

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

My jk.conf file (based on /usr/share/doc/packages/apache2-mod_jk/jk.conf) is


# simple configuration for apache (for AJP connector, modul mod_jk.so)

<IfModule mod_jk.c>

    JkWorkersFile /usr/share/tomcat6/conf/jk/workers.properties
    JkLogFile /var/log/apache2/mod_jk.log

    # Log level to be used by mod_jk
    JkLogLevel info

    # The following line makes apache aware of the location of
    # the /examples context
    Alias /examples "/srv/tomcat6/webapps/examples"
    <Directory "/srv/tomcat6/webapps/examples">
        Options Indexes FollowSymLinks
        allow from all
    </Directory>

    # The following line mounts all JSP files and the /servlet/ uri to tomcat
    JkMount /*/*.jsp worker1
    JkMount /examples/jsp/jsp2/el/basic-arithmetic.jsp worker1
    # JkMount /examples/* worker1
    # JkMount /examples/jsp/* worker1
    # JkMount /examples/servlet/* worker1

    # The following line prohibits users from directly accessing WEB-INF
    <Location "/examples/WEB-INF/">
        AllowOverride None
        deny from all
    </Location>

</IfModule>

(I experimented with the JkMount field, but none worked.)

Apache seems to read the jk.conf file well, or it would not know about the jsp examples. However, the jsp are just returned as plain text without being processed by jasper, so it seems like tomcat doesn’t handle the requests.

All help is appreciated.

Best regards, Mike

I’ll give you the files (slightly modified) from a working config, and you can nut out yourself what you have missed. Good luck.

jk.conf:

<IfModule mod_jk.c>

    JkWorkersFile /etc/tomcat6/workers.properties
    JkLogFile /var/log/tomcat6/mod_jk.log
    JkShmFile /var/log/tomcat6/shm

    # Log level to be used by mod_jk
    JkLogLevel error

    # The following line makes apache aware of the location of
    # the /showcase context
    Alias /showcase "/srv/tomcat6/webapps/showcase"
    <Directory "/srv/tomcat6/webapps">
        Options Indexes FollowSymLinks
        Allow from All
    </Directory>

    # The following line mounts all JSP files and the /showcase/ uri to tomcat
    JkMount /showcase/* ajp13

    # The following line prohibits users from directly accessing WEB-INF
    <Location "/showcase/WEB-INF/">
        AllowOverride None
        Deny from All
    </Location>

</IfModule>

worker.properties, just the relevant part:


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

Hello ken_yap,

Thanks for your reply.

I copied your files into my system, but the problem persists: I get to see the jsp source.

Any other ideas??

Thanks again. Mike.

I suspect that you have not really loaded mod_jk and Apache is serving the files without proxying to Tomcat. Is it mentioned in /etc/sysconfig/apache2, in variable APACHE_MODULES? Do this test:

/usr/sbin/httpd2 -M

If jk_module is not on the list, then that is the problem. You can also check the Tomcat logs. There should be an entry in the log for each request passed through from Apache.

I figured out what was wrong: VirtualHosts.

I configured two virtual hosts in apache. The tomcat connector (mod_jk) was included in httpd.conf in the top level, outside of a virtual host definition. I was assuming that calls to http://localhost/ would NOT be handled by a virtual host if there was no name-matching virtual host.

That was wrong, ALL requests are handled by virtual hosts if you define them. If no matching host is found, the first one listed is used.

Hence, moving the jk config inside a virtual host element solved the problem. So, I included the following in a VirtualHost element.


  <IfModule mod_jk.c>
        # The followingV line makes apache aware of the location of
        # the /examples context
        Alias /examples "/srv/tomcat6/webapps/examples"
        <Directory "/srv/tomcat6/webapps/examples">
            Options Indexes FollowSymLinks
            Allow from All
        </Directory>

  	# Mounted stuff goes via tomcat (ajp13) 
  	JkMount /examples/* ajp13
  	# You can explicitly unmount part of the mounted tree
  	# Unmounted stuff goes via apache.
  	# Serve html, jpg and gif using httpd
  	JkUnMount /examples/*.html ajp13
  	JkUnMount /examples/*.jpg  ajp13
  	JkUnMount /examples/*.gif  ajp13
    
        # The following line prohibits users from directly accessing WEB-INF
        <Location "/examples/WEB-INF/">
            AllowOverride None
            Deny from All
        </Location>
</IfModule>

Is it handy to keep some configuratio global, included directly from httpd.conf:


<IfModule mod_jk.c>

    JkWorkersFile /usr/share/tomcat6/conf/jk/workers.properties
    JkLogFile /var/log/tomcat6/mod_jk.log
    JkShmFile /var/log/tomcat6/shm

    # Log level to be used by mod_jk
    JkLogLevel info

</IfModule>

That did the trick for me…