Ruby on Rails and Tomcat
I recently came across a problem of having to get Ruby on Rails running on Apache (port 80) communicate with Tomcat (port 8080.) I learned that there are numerous ways to get this done. The first approach was to use a connector, and Kyle got mod_jk set up and running so that Apache would connect to Tomcat with a particular URL. That all worked well, until I took the static HTML prototype and got it under the control of Rails.
I set up a virtual host running on port 80 to go to my Rails public directory, which worked fine. But once Rails takes control, the connector set up with mod_jk no longer worked; instead I just get a Rails error. Boo hoo.
I gave up on the connector approach. It seemed kind of flaky anyway. I really wanted to centralize where all this configurations were happening, and I wanted it out of the httpd.conf file, since I never feel comfortable modifying it. The best place would be inside the .htaccess file with the public directory, where all the other rewrite rules live.
I thought about having to create a webservice or some sort of proxy, but I learned about mod_proxy. It’s really easy to use, and gives you a new switch [P] in your rewrite rule. Enable it in httpd.conf and you can use it within the .htaccess file.
Your rewrite rule would look something like this:
RewriteRule ^tomcat-dir/(.*)$ http://localhost:8080/tomcat-dir/$1 [P]
My job here is done…
