Rails5 + ActionCable:与'ws:// {hostname} / cable'的WebSocket连接失败:WebSocket在建立连接之前关闭

问题描述 投票:9回答:1

当我连接到我的Rails 5 + ActionCable应用程序时,我在Chrome浏览器中获得WebSocket connection to 'ws://{hostname}/cable' failed: WebSocket is closed before the connection is established.。在我的本地开发环境中,它不会发生。它只发生在我的临时和生产环境中。

Successful screenshot on development

Failed screenshot on staging

我使用Amazon ElasticBeanstalk来部署我的应用程序。在使用chrome开发人员工具监控请求之后,我发现与开发环境不同,WebSocket请求在登台,生产环境中没有得到响应(待定),因此ActionCable不断尝试连接到WebSocket端点(ws:// {hostname} /电缆)。

问题是,在我的puma.log中,它说的是这样的:

I, [2016-07-26T13:45:53.921154 #32369]  INFO -- : Registered connection (Z2lkOi8vYXNrLWNvLWRlL1VzZXIvMg)
I, [2016-07-26T13:46:05.775788 #32369]  INFO -- : Finished "/cable/" [WebSocket] for 121.166.105.106 at 2016-07-26 13:46:05 +0000
I, [2016-07-26T13:46:18.074895 #32369]  INFO -- : [917fd706-4f1a-4178-bd95-7a33c0c7b621] Started GET "/cable" for 121.166.105.106 at 2016-07-26 13:46:18 +0000
I, [2016-07-26T13:46:18.075764 #32369]  INFO -- : [917fd706-4f1a-4178-bd95-7a33c0c7b621] Started GET "/cable/" [WebSocket] for 121.166.105.106 at 2016-07-26 13:46:18 +0000
I, [2016-07-26T13:46:18.075817 #32369]  INFO -- : [917fd706-4f1a-4178-bd95-7a33c0c7b621] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)

也就是说,升级到WebSocket是成功的,但其余的是未知的。你有解决这个问题的线索吗?

ruby-on-rails web-services elastic-beanstalk actioncable
1个回答
0
投票

由于问题的作者已经评论过websocket连接中这种失败的可能原因是由于在错误配置的Loadbalancer后面的请求重写。

例如,我遇到了一个配置错误的Apache服务器,其中有重写规则,用于将使用的协议从http重写为ws,用于rails应用程序。

如果它帮助其他人在这里是工作的Apache VHost配置,在这种情况下启用websockets。

<VirtualHost domain.of.the.rails-app:80>
    ServerName domain.of.the.rails-app
    DocumentRoot /var/www/apps/rails-app/public
    ProxyPreserveHost On
    ProxyPass /error-documents !
    ErrorDocument 503 /error-documents/503.html
    Alias /error-documents /var/www/apps/rails-app/public
    ProxyPass / http://0.0.0.0:3000/
    ProxyPassReverse / http://0.0.0.0:3000/
    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} websocket [NC]
    RewriteRule /(.*) ws://localhost:3000/$1 [P]
</VirtualHost>
© www.soinside.com 2019 - 2024. All rights reserved.