【问题标题】:Websocket-rails doesn't work on production evironment with Nginx and UnicornWebsocket-rails 不适用于 Nginx 和 Unicorn 的生产环境
【发布时间】:2014-06-15 08:16:05
【问题描述】:

我有带有 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

【问题讨论】:

  • 你解决过这个问题吗?
  • @jay 我能够通过使用 Thin 作为通用 http 服务器来解决这个问题,而无需独角兽。但是对于 unicorn + thin(for websocket) + nginx 我还没有找到解决方案
  • 你在哪个端口上运行 Nginx?您的客户端代码正在调用 3001,但随后您也代理到 3001。 Nginx 和 Thin 不可能在同一台服务器上同时提供 3001。
  • Nginx 在 80 上运行。unicorn 是套接字,在 3001 上很瘦。

标签: ruby-on-rails ruby nginx websocket unicorn


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2013-11-13
  • 2018-01-06
  • 2014-04-13
  • 2018-12-15
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
相关资源
最近更新 更多