Using Nginx as a front-end for Railo

Railo is a wonderful open-source CFML engine offering outstanding performance combined with a small footprint. In the past I usually served Railo applications using Apache 2 in a reverse-proxy configuration as it was relatively painless to configure and maintain although Apache could easily turn memory hungry under load. Right now I am running a production test using Nginx as a front-end webserver instead of Apache and I must say it rocks.

Serving Railo application through Nginx is nothing else than adding a couple of rows of settings to the your /usr/local/conf/nginx.conf settings file. Let’s take a look at the settings on my development machine (MacBook Pro with Mac OSX 10.7 Lion).

  • Railo is installed and fully configured with Tomcat and runs on port 8080.
  • Nginx is installed and running
  • The root of my Railo application is in /Developer/Railo/tomcat/webapps/myrailoapp.local
  • myrailoapp.local points to 127.0.0.1
  • I want to serve my Railo app on port 80 using myrailoapp.local as URL and Nginx as my front-end webserver.
  • Port 80 is currently free and is not in use.
  • We are going to serve all Railo traffic (including static files) through Nginx for the sake of this post.

Open your /usr/local/conf/nginx.conf settings file in your favorite editor.
Add a new virtual server:

    server {
        listen       80;
        server_name  myrailoapp.local;
 
        location / {
                index  index.cfm ;
                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;
       }
 
    }

Save the settings file.
To validate your settings file type in Terminal:

sudo /usr/local/sbin/nginx -t -c /usr/local/conf/nginx.conf

If you did everything correctly, Nginx will confirm that the settings file is usable:

nginx: configuration file /usr/local/conf/nginx.conf test is successful

Restart your Nginx. First find the PID of the Nginx master process:

ps -ef|grep nginx
 
#You will see something like this after you hit the Enter key:
 
$ ps -ef|grep nginx
    0  2787     1   0 10:28pm ??         0:00.00 nginx: master process /usr/local/sbin/nginx
   -2  2788  2787   0 10:28pm ??         0:00.03 nginx: worker process
  502  9496   697   0  9:17am ttys003    0:00.00 grep nginx

Kill the process using the PID and start Nginx:

sudo kill 2787
sudo /usr/local/sbin/nginx

Test your setup by entering http://myrailoapp.local in your browser
Congratulations! You are serving your Railo app using Nginx as a reverse proxy.