Tuesday, April 21, 2009

Configuring nginx for both Rails and Tomcat

This is easy to do but I had a few missteps in setting up nginx with both Rails and Tomcat so it is worthwhile documenting what I did. I have two cooking and recipe web portals, an older one written in Java (CJsKitchen.com) and a newer one that uses the USDA nutritional database that runs on Rails (CookingSpace.com). These are low volume sites so I wanted to run both on a single inexpensive VPS.

Usually I run a few virtual domains on one Tomcat instance (requires a simple mapping in conf/server.xml between virtual domain names and subdirectories of $TOMCAT/webapps) but in this case, I only need to add a single virtual domain for my Java web app on a system that is already configured for Rails. So, no changes to server.xml are required and I deployed to $TOMCAT/webapps/ROOT. Then, the only thing left to do was add a section in my nginx.conf file for Tomcat running on port 8080:
    server {
listen 80;
server_name cjskitchen.com www.csjkitchen.com;
root /usr/local/tomcat/webapps/ROOT;

access_log off;
rewrite_log on;

location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Both my wife and I are very good cooks, and writing two cooking/recipe web apps has been a fun hobby.

3 comments:

Derek said...

Curious why you chose Ruby for your newer implementation. Just looking for a change of pace or were there technical reasons?

Mark Watson, author and consultant said...

Hello Derek,

Partially as a change of pace, but I find that I can develop and maintain Rails web applications with much less effort than Java. That said, I still very much like the Java platform when runtime performance is more important than reducing the expense of development.

-Mark

ssa said...

Just wanted to add a comment - the nginx.conf file worked fine.

You saved me a lot of hassle, because previously I was following another recipe for an nginx.conf file which was more complicated:

http://www.ruby-forum.com/topic/164215

Also, that other nginx.conf file wasn't serving my static content (CSS, JavaScript) so it clearly was incorrect.

Your nginx.conf was shorter and simpler - and correct. Thanks!