我正在将Jelastic用于我的开发环境(尚未投入生产)。
我的应用程序与Unicorn一起运行,但是我发现了带有ActionCable的websocket,并将其集成到我的应用程序中。

在本地,一切正常,但是当部署到我的Jelastic环境(使用默认的NGINX / Unicorn配置)时,我在javascript控制台中收到此消息,而访问日志中什么也看不到

WebSocket connection to 'ws://dev.myapp.com:8080/' failed: WebSocket is closed before the connection is established.


我曾经在本地环境中使用,并通过在配置文件中添加所需的ActionCable.server.config.allowed_request_origins来解决此问题。所以我仔细检查了我的开发配置,没关系。

这就是为什么我想知道是否有NGINX配置的特定内容,而不是ActionCable git页面上的说明

bundle exec puma -p 28080 cable/config.ru


对于我的应用程序,我遵循了enter link description here中的所有内容,但未提及有关NGINX配置的任何内容

我知道带有ActionCable的websocket是相当新的,但我希望有人能够在这方面给我带头

非常感谢

最佳答案

好的,所以我终于设法解决了这个问题。以下是使这项工作有效的不同步骤:

1.nginx:我真的不知道这是否需要,但是当我的应用程序与Unicorn一起运行时,我将其添加到了我的nginx conf中

upstream websocket {
  server 127.0.0.1:28080;
}

server {
  location /cable/ {
    proxy_pass http://websocket/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }
}


然后在我的config/environments/development.rb文件中:

config.action_cable.url = "ws://my.app.com/cable/"


2.允许请求的来源:然后我注意到即使我在ActionCable.server.config.allowed_request_origins文件中使用config/environments/development.rb,我的连接也被拒绝了。我想知道这是否不是由于文档中所述的默认开发http://localhost:3000而导致的。所以我添加了这个:

ActionCable.server.config.disable_request_forgery_protection = true


我还没有生产环境,因此我尚无法测试它的状态。

3.Redis密码:如文档中所述,我使用的是config/redis/cable.yml,但出现此错误:

Error raised inside the event loop: Replies out of sync: #<RuntimeError: ERR operation not permitted>
/var/www/webroot/ROOT/public/shared/bundle/ruby/2.2.0/gems/em-hiredis-0.3.0/lib/em-hiredis/base_client.rb:130:in `block in connect'


因此,我知道为Redis服务器设置密码的方式不好。

实际上,您必须执行以下操作:

development:
  <<: *local
  :url: redis://user:password@my.redis.com:6379
  :host: my.redis.com
  :port: 6379


现在一切正常,Actioncable确实令人印象深刻。

也许我的一些问题很琐碎,但我正在分享它们以及我如何解决它们,以便每个人都可以根据需要选择

09-09 23:59
查看更多