Separating static from dynamic traffic with Nginx and Railo

I’ve blogged about a basic Nginx/Railo setup yesterday. Let’s take our setup a step further: we want Railo to process dynamic CFML content only and leave caching and serving static files (stylesheets, JavaScript etc) to Nginx. To do this, we’ll split our single Location container into two containers using pattern match:

  • One serving static files (gif,jpg,png,js,ico,css,pdf)
  • One serving dynamic content created by Railo (.cfm)

My assumptions are:

  • Railo is installed and fully configured with Tomcat and runs on port 8080.
  • Nginx is installed and running
  • We serve static content from /Developer/Railo/tomcat/webapps/myrailoapp.local/static
  • myrailoapp.local points to
  • I want to serve my Railo app on port 80 using myrailoapp.local as URL with Nginx as my front-end webserver.
  • Port 80 is currently free and is not in use.

That will make our virtual host configuration look like this:

    server {
       listen       80;
       server_name  myrailoapp.local;
       location ~ \.(gif|jpg|png|js|ico|css|pdf)$ {
                expires 30d;
                root /Developer/Railo/tomcat/webapps/myrailoapp.local/static;
                proxy_cache_valid    200  1d;
                proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
        location ~\.cfm$ {
                index  index.cfm ;
                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  12787     1   0 13:28pm ??         0:00.00 nginx: master process /usr/local/sbin/nginx
   -2  12788  2787   0 13:28pm ??         0:00.03 nginx: worker process
  502  9496   697   0  14:17am ttys003    0:00.00 grep nginx

Kill the process using the PID and start Nginx:

sudo kill 12787
sudo /usr/local/sbin/nginx

Test your setup by entering http://myrailoapp.local in your browser
Congratulations! Nginx is now serving cached static content and Railo is serving dynamic pages only.