websocket-rails: Websocket-rails doesn't work on production evironment with Nginx ang Unicorn

I have Rails 3.2 application with gem websocket-rails 0.7.

On development machine, all work fine

On production enviroment, I use Nginx/1.6 as proxy server. Thin is used on standalone mode (following https://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server-Mode).

nginx config:

location /websocket/ {
   proxy_pass http://localhost:3001/websocket;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}

On backend side, I have the following code for send notification to clients

WebsocketRails[:callback_requests].trigger 'new', call_request

On client side, I got a connection using:

dispatcher = new WebSocketRails window.location.host + ':3001/websocket'
channel    = dispatcher.subscribe 'callback_requests'

But notification doesn’t come to the client.

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Comments: 35 (2 by maintainers)

Most upvoted comments

Solved!

Nginx:

location /websocket {
 proxy_pass http://localhost:3001/websocket;
 proxy_http_version 1.1;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection "Upgrade";
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header Host $http_host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-Proto https;
 proxy_redirect off;
}

javascript, i have to change to false to use http:

var websocket = new WebSocketRails(window.location.hostname + '/websocket', false);

Try to set location like below:

location = /websocket {

Here is my nginx config

 upstream backend {
    server localhost:3001; # Thin server is running on port 3001 (Stand alone server)
  }


  server {
    listen       443;
    ssl on;
    ssl_certificate PATH_TO_CERTS;
    ssl_certificate_key PATH_TO_KEY;

    server_name  example.com;
    ssl_session_timeout 5m;
    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location /websocket {
      proxy_pass http://localhost:3001/websocket;
      proxy_http_version 1.1;
      proxy_set_header  X-Real-IP  $remote_addr;
      add_header Access-Control-Allow-Origin *;                                                                                
      proxy_set_header Upgrade websocket;
      proxy_set_header Connection upgrade;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    root PATH_TO_APP_PUBLIC;
    passenger_enabled on;
    rails_env production;
}

And the connection to ws is made like this ws://example.com/websocket

Also by default config.force_ssl is always false. So If its commented out, it should be fine.

Let me know if that helps.

It’s a long shot, but if i remember correctly. Some version of nginx are case sensitive on the “Upgrade” section.

Here’s a copy and paste from an app I have in production.

  location /websocket {
    proxy_pass http://rails_upstream/websocket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade websocket;
    proxy_set_header Connection Upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }