ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境

标签 ruby-on-rails ruby nginx websocket unicorn

我有带有 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";
}

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

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

在客户端,我使用以下方法建立连接:

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

但通知不会到达客户端。

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

最佳答案

您的 nginx 配置将 /websocket/ 下的请求与结尾的 / 匹配。那就是/websocket/blah的目录组件。

如果您查看您的 nginx 访问日志文件,您会发现您对 /websocket 的请求被 301 重定向到 /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";
}

关于ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363874/

相关文章:

ruby-on-rails - ruby 中的每个循环都不起作用

ruby-on-rails - Rails routing with date 并且有很多

ruby-on-rails - 清理文本中的字符并设置样式

nginx - 如何配置nginx的default.conf

ruby-on-rails - 设计和公寓 gem Multi-Tenancy 应用程序

jquery - 暂时禁用 Rails 远程链接

ruby-on-rails - Rails - CSV 导出 : prompt for file download

ruby-on-rails - 如何在 Controller 规范中禁用 before_action?

nginx - 在istio入口网关中启用端口

NGINX:当 proxy_pass 失败时提供静态文件