Nginx and Coldfusion - Using Nginx as a reverse proxy for more performance

10/21/2009
0

The usual setup is ColdFusion on either Windows with IIS or on Linux with Apache.

Both webservers are great webservers unless you get excessive load on them. At that time you wish that you had one server to do all the static files and one or more servers to do the ColdFusion requests.

Enter Nginx, the lightweight webserver written by Igor Sysoev. Nginx is one of the fastest webservers around and due to its different architecture will yawn at the load while Apache is already collapsing.

I just want to post my two config files to show how Nginx can be configured to work as a reverse proxy.


The main nginx.conf file:

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
        worker_connections  1024;
}

http {
        open_log_file_cache max=1000 inactive=20s min_uses=2 valid=5m;
        include     /etc/nginx/mime.types;
        include     /etc/nginx/proxy.conf;
        access_log  /dev/null;
        proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=8g;
        sendfile        on;
        #tcp_nopush     on;
        
        #keepalive_timeout  0;
        keepalive_timeout  65;
        tcp_nodelay        on;
        
        gzip  on;
        gzip_disable  msie6;
        
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

This is pretty much the default config file except for the proxy_cache_path statement.

And here a config file from the sites-enabled folder:

server {
        listen 123.45.67.89:80;
        server_name  some.domain.com;
        access_log  /var/log/nginx/some.domain.com.log;
        
        location ~ \.(gif|js|ico|txt)$ {
                expires 360d;
                proxy_pass           http://192.168.1.100:8080;
                proxy_cache          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$ {
                proxy_pass  http://192.168.1.100:8080;
        }
}

This is it. With this config every request to the domain some.domain.com will serve .gif, .js, .ico and .txt files from the local Nginx cache which will refresh after one day (1d). All .cfm files will be passed to the ColdFusion server. Every other file type (e.g. .jpg, .php) will result in a 404 that will never even reach the ColdFusion server. And this config could be enhanced to have n-ColdFusion servers handle the requests. By using this you can leave your current CF servers as they are and store the static files on them as you do now. Nginx will contact your CF server for each static file only once a day.

Of course this config could be used for ASP, PHP or any other application server as well, just change the .cfm to whatever you like.

If you've read this far you might as well follow me on Twitter here.


Comments