Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket-rails doesn't work on production evironment with Nginx and 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 and Unicorn as http 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.

Related issue on github - github.com/websocket-rails/websocket-rails/issues/211

like image 946
MaxKonin Avatar asked Apr 29 '14 11:04

MaxKonin


1 Answers

Your nginx config is matching requests below /websocket/ with the trailing /. That is the directory component of /websocket/blah.

If you look in your nginx access log file you'll find your requests to /websocket are being 301 redirected to /websocket/.

Remove the trailing /

location /websocket {
   proxy_pass http://localhost:3001/websocket;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}
like image 189
Matt Avatar answered Oct 27 '22 07:10

Matt