Using Nginx Web Server with ColdFusion

One of the many sites I run is a ColdFusion site.  Not by choice, but because the developer that created the site was a CF developer.  When I acquired the site, the original developer would just run ColdFusion Server as a standalone web and application server.  For most cases this is fine, but as the site has grown, so has the traffic.  When you think of all the traffic that is going to the ColdFusion application server, not just dynamic CFM file, but lots of static content like images, CSS, and plain HTML files.  There is no reason why we need to put the extra stress on the ColdFusion server, when a simple webserver could handle the static content, and pass all the requests for dynamic content to ColdFusion.

The original setup was just ColdFusion running on port 80 which is the standard port when going to http://www.mydomain.com.  So first I needed to change ColdFusion’s webserver to run on a different port.  To do that you need to edit the jrun.xml file which on most Linux/Unix systems is located in cf_root/runtime/servers/coldfusion/SERVER-INF.  cf_root is the main directory where you installed ColdFusion.  In the jrun.xml file, you are looking for the section that says:

<!-- ================================================================== -->
 <!-- This is the built-in JRun Web Server                               -->
 <!-- ================================================================== -->
 <service name="WebService">
 <attribute name="port">80</attribute>
 <attribute name="interface">*</attribute>
 <attribute name="deactivated">false</attribute>
 <attribute name="activeHandlerThreads">100</attribute>
 <attribute name="minHandlerThreads">1</attribute>
 <attribute name="maxHandlerThreads">1000</attribute>
 <attribute name="mapCheck">0</attribute>
 <attribute name="threadWaitTimeout">300</attribute>
 <attribute name="backlog">500</attribute>
 <attribute name="timeout">300</attribute>
 </service>

Where is says port 80, you want to change that to another port like 8080.  Next we need to configure Nginx.  On most RedHat/CentOS based systems the Nginx config’s are in /etc/nginx and we’re looking for the nginx.conf.  Here is the basic Nginx config to server your website on port 80, then pass all the requests for ColdFusion files to ColdFusion on port 8080.  This config has some of the basic Nginx settings stripped out and just gives you the server section, this also assumes that you have installed ColdFusion in the /usr/local/coldfusionmx7 directory, and /usr/local/coldfusionmx7/wwwroot is where all your content is.

server {
  listen       80;
  server_name  _;
  access_log  /var/log/nginx/host.access.log  main;

  location / {
    root   /usr/local/coldfusionmx7/wwwroot;
    index  index.cfm index.html index.htm;
    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;         
  }

  location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|
exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
    root /usr/local/coldfusionmx7/wwwroot;
  }
}

At this point, all you need to do is restart ColdFusion, you should be able to verify it still works by going to http://www.mydomain.com:8080. Then you can start Nginx, and go to http://www.mydomain.com.

One Response

  1. Sami Hoda Says:

    Excellent post. I’ve also written about CF and NGINX.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


Switch to our mobile site