使用Nginx和Unicorn,Websocket-rails不适用于生产环境

Max*_*nin 6 ruby ruby-on-rails nginx unicorn websocket

我有使用gem websocket-rails 0.7的Rails 3.2应用程序.

在开发机器上,一切正常

在生产环境中,我使用Nginx/1.6作为代理服务器,使用Unicorn作为http服务器.Thin用于独立模式(遵循https://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server-Mode).

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";
}
Run Code Online (Sandbox Code Playgroud)

在后端方面,我有以下代码用于向客户发送通知

WebsocketRails[:callback_requests].trigger 'new', call_request
Run Code Online (Sandbox Code Playgroud)

在客户端,我使用以下连接:

dispatcher = new WebSocketRails window.location.host + ':3001/websocket'
channel    = dispatcher.subscribe 'callback_requests'
Run Code Online (Sandbox Code Playgroud)

但通知不会发给客户.

关于github的相关问题 - github.com/websocket-rails/websocket-rails/issues/211

Mat*_*att 5

您的nginx配置匹配以下请求/websocket/与尾随/.那是目录组件/websocket/blah.

如果您查看nginx访问日志文件,您会发现您的请求/websocket被重定向到/websocket/.

删除尾随 /

location /websocket {
   proxy_pass http://localhost:3001/websocket;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}
Run Code Online (Sandbox Code Playgroud)