modJK + modRewrite + tomcat 5 OR jboss 4
Cardwell Cupp - 04/10/2008
Last week I spent way too long sorting out a somewhat trival problem. Adding
some rewrite rules to our J2EE applicaiton. Our applicaiton is clustered using
modJk's loadbalancer (roundrobin / failover). Up until this point, we've only
accepted traffic over http. We reached a point to that we wanted all traffic
over http. So I installed the certificate in the proper place and the application
was going.
The next responsible step was to make sure all traffic initiated over port 80
was redirected to 443. It was quite frustrating; nothing seemed to work.
Finally I got it to work. Here are the instructions.
Loading The Modules (Order is Important)
For some reason, order matter. I looked a bit at the apache hooks for each of
these mods, and it wasn't clear to me why order should matter.
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
Add in the ForwardURICompat (Warning)
As noted in the Apache modJk documentation (http://tomcat.apache.org/connectors-doc/reference/printer/apache.html):
"Using JkOptions ForwardURICompat, the forwarded URI will be decoded by Apache httpd. Encoded characters will be decoded and explicit path components like ".." will already be resolved. This is less spec compliant and is not safe if you are using prefix JkMount. This option will allow to rewrite URIs with mod_rewrite before forwarding."
So, add the following options to your Jk config. I'm using Ubuntu so its in the jk.conf file that I added mine.
JkOptions +ForwardURICompat
Add your rewrites in your VHost
I'll be the first to tell you that I'm don't know much about apache / rewrite / modjk. Through trial and error
I got it working for me. THe thing that I found weird about this solution is that no matter what I
stuck in my .htaccess file, the urls all behaved the same. It was like it was ignoring it. The only way
I could get it to work is to stick the rewrite rules and engine settings in the vhosts where I wanted it.
RewriteEngine on
RewriteLog /tmp/rewrite.log
RewriteLogLevel 9
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
There you have it. Go ahead and reload or restart your apache and give er' a whirl. Let me know
if you need some help. I'll try to help if I can. (cardwellcupp@gmail.com)
|