RUBY ON RAILS - Can nginx be used as a reverse proxy for a backend websocket server?

We're working on a Ruby on Rails app that needs to take advantage of html5 websockets. At the moment, we have two separate "servers" so to speak: our main app running on nginx+passenger, and a separate server using Pratik Naik's Cramp framework (which is running on Thin) to handle the websocket connections.

Ideally, when it comes time for deployment, we'd have the rails app running on nginx+passenger, and the websocket server would be proxied behind nginx, so we wouldn't need to have the websocket server running on a different port.

Problem is, in this setup it seems that nginx is closing the connections to Thin too early. The connection is successfully established to the Thin server, then immediately closed with a 200 response code. Our guess is that nginx doesn't realize that the client is trying to establish a long-running connection for websocket traffic.

Admittedly, I'm not all that savvy with nginx config, so, is it even possible to configure nginx to act as a reverse proxy for a websocket server? Or do I have to wait for nginx to offer support for the new websocket handshake stuff? Assuming that having both the app server and the websocket server listening on port 80 is a requirement, might that mean I have to have Thin running on a separate server without nginx in front for now?

Thanks in advance for any advice or suggestions. :)

-John

This question and answers originated from www.stackoverflow.com
Question by (3/10/2010 6:07:32 PM)

Answer

You can't use nginx for this currently, but I would suggest looking at HAProxy. I have used it for exactly this purpose.

The trick is to set long timeouts so that the socket connections are not closed. Something like:

timeout client  86400000 # In the frontend
timeout server  86400000 # In the backend

If you want to serve say a rails and cramp application on the same port you can use ACL rules to detect a websocket connection and use a different backend. So your haproxy frontend config would look something like

frontend all 0.0.0.0:80
  timeout client    86400000
  default_backend   rails_backend
  acl websocket hdr(Upgrade)    -i WebSocket
  use_backend   cramp_backend   if websocket

For completeness the backend would look like

backend cramp_backend
  timeout server  86400000
  server cramp1 localhost:8090 maxconn 200 check
Answer by

Find More Answers
Related Topics  ruby-on-rails  nginx  websocket  reverse-proxy  thin
Related Questions
  • nginx be both reverse proxy and web server

    I currently use nginx with passenger to serve my rails app. considering including a caching reverse proxy to the equation. Can I use the same instance of nginx as a reverse proxy (running on port 80…
  • nginx as a reverse caching proxy

    Is it caching static content? I check the /tmp dir where the location is set but it's empty. nginx.conf user nobody; # no need for more workers in the proxy mode worker_processes 1; error_log …
  • nginx caching as a reverse proxy

    Am I caching right? is there any way i can improve this? the site gets about 3k unique hits a day nginx.conf: gzip_types text/* application/* image/*; open_file_cache max=1000 inactive=20s;…
  • Setting nginx as a reverse proxy help

    I'm a bit new to reversing caching proxy.. Just wanting to setup a on my ssl requests, The secure.mydomain mainly deliverys txt and gz static small (less than 200k) files. Any help would be greatly …
  • Nginx + php-fpm VS Nginx as a reverse proxy for Apache

    Can someone enlighten me whats the advantage of having NginX as a reverse proxy to Apache. People suggest it so that static content is handled by nginx and dynamic content (php files) are handed ove…
  • nginx - reverse proxy, multiple thin instances, cache

    I'm using nginx to serve a sinatra based project which has fairly static content.. I've tried to combine this: http://articles.slicehost.com/2008/5/27/ubuntu-hardy-nginx-rails-and-thin with th…
  • Nginx as reverse proxy

    How can I setup nginx as reverse proxy along with apache on CentOS 5.6 I have already install apache, but I dont know if I should proceed with installing nginx because I am afraid that may set ng…
  • Nginx as Reverse Proxy for Apache

    I have nginx running as reverse proxy for apache for my website. The problem I have is that all the logs in apache have 127.0.0.1 as the IP address. I have these set in nginx.conf proxy_set_heade…
  • How to set up Nginx as a caching reverse proxy?

    I heard recently that Nginx has added caching to its reverse proxy feature. I looked around but couldn't find much info about it. I want to set up Nginx as a caching reverse proxy in front of Apa…
  • Websocket Server Behind IIS 7 Reverse Proxy

    I've build a WebSocket chat server using the hybi-17 spec using .NET 4.0 and Sockets. If the browser connects to the chat server on port 81, everything functions as it should. However due to company…