Nginx proxy for Railo with web sockets

Aug 20 2013

Nginx proxy for Railo with web sockets

Posted by AJ Mercer at 9:56 AM Web Sockets | Nginx | Railo

I have been tinkering with the idea of adding web sockets to a new build of a web site at work so I can push data to the browser. The first obstacle I had to get around was the corporate firewall and navigate through the DMZ. I thought I was going to have to get the network administrator to poke holes in the firewall and add more rules. But then I discovered Nginx can proxy web sockets on port 80!

Here are some links that I collected during my R&D

Railo Web Socket Extention

Railo Proxy ignoring static resources

Websocket Proxy

significant part of nginx.conf

    server {
        listen        80;
        server_name    dev.local;
        index          index.cfm index.html;
        root        C:\Inetpub\websites\dev.locsl\webroot;
        
        location / {
            try_files $uri $uri/ @farcry;
        }
        location /webtop {
            alias C:\Inetpub\websites\dev.local\webroot\farcry\core\webtop;
            try_files $uri $uri/;
        }
        
        location @farcry {
            rewrite ^/(.*)? /index.cfm?furl=$1 last;
       }
       
        location ~* \.(cfm|cfml|cfc|jsp|cfr)(.*)$ {
            proxy_pass          http://dev.local:8888;

            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;

            proxy_set_header    X-Forwarded-Host $host;
            proxy_set_header    X-Forwarded-Server $host;
            proxy_set_header    X-Real-IP $remote_addr; 
                # Tomcat Server.xml : 
                # <Valve className="org.apache.catalina.valves.RemoteIpValve"  />
           }
           
#### Web Sockets
## old JS
## ws = new WebSocket('ws://dev.local:10126');
## new JS
## ws = new WebSocket('ws://dev.local:80/stock');

        location /stocks {
            access_log off;
            proxy_pass http://dev.local:10126;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # WebSocket support (nginx 1.4)
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            
            # Path rewriting
            rewrite /stocks/(.*) /$1 break;
            proxy_redirect off;
        }
        
        location /chat {
            access_log off;
            proxy_pass http://dev.local:8000;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # WebSocket support (nginx 1.4)
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            
            # Path rewriting
            rewrite /chat/(.*) /$1 break;
            proxy_redirect off;
        }        
    }