Websocket-rails doesn't work on production evi

2019-04-28 03:10发布

问题:

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

回答1:

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";
}