部署 [问题] 部署的时候碰到的一些问题

hesongGG · September 23, 2016 · Last by hesongGG replied at February 18, 2017 · 9507 hits
  • 遇到的问题:
    1. 部署 actioncable 到正式环境的时候,在谷歌 console 里报错为: WebSocket connection to 'ws://example.com:28080/' failed: WebSocket is closed before the connection is established. 报错信息的大致意思.
    2. 但是在正式服务器上,cable 的 process 是正常运行的,log 里没有相关的 websocket 信息输出。
    3. ps: 在测试服务器上没有出现这种错误。是可以正常运行的。
  • 相关的环境
    1. 测试服务器是阿里云,
    2. 正式服务器是 ucloud
    3. 跑的应用服务器是 puma
    4. rails4
  • 相关处理:
    1. 看了华顺的那个关于 ActionCable 部署的细节经验分享.也改了关于限制的问题,还是一样的报错。
    2. 把正式环境还有测试环境的进程全部杀死。然后重新部署在正式环境,测试环境禁用 cable,还是报一样的错误。 找了很久的谷歌,没有什么信息。
    3. 谷歌了挺久的,尝试了之后都不可以。
在正式服务器上,当杀死 cable 的进程之后,报错信息也是
WebSocket connection to 'ws://example.com:28080/' failed: WebSocket is closed before the connection is established.
在本地或者测试服务器的时候,关闭 cable,会直接报错为:
WebSocket connection to 'ws://localhost:28080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
疑问:同样是关闭了 cable 的进程,正式环境和测试环境以及本地的报错信息为何会有这样的差异?

出现这种错误是因为什么原因呢,因为测试环境和正式环境的配置,基本是相同的。

stand alone 跑的时候 action cable 要设置同源策略。可以修改去掉限制

config.action_cable.allowed_request_origins = [ /https?:\/\/.*/ ]

或者参考

https://ruby-china.org/topics/28935

的 nginx 的配置。

#1 楼 @gyorou 同源策略应该报的不是这个错

example.com,这个地址是特意改的吗

#1 楼 @gyorou

  1. 都在指定的一个域名下面,都是同源了吧。并且我也禁止了跨域的检查

    config.action_cable.disable_request_forgery_protection = true
    
  2. nginx 的配置应该是没有问题的。同测试环境的配置是差不多的。

#3 楼 @jun1st 恩,这里是特意改的。

port 不一样的话不算同源啊。。。 不过既然允许跨域了,线上是不是wswss的问题呢。

#6 楼 @gyorou actioncable Consumer Configuration

If you host your production server via https, you will need to use the wss scheme for your Action Cable server.

我这里的是 http 协议。ws 应该是正常的。

WebSocket is closed before the connection is established.

报错的意思大致是:

In this code what it means is that ws.close() was called (by user code) before the connection was even given a chance to be established.

大概的错误表示为:在连接之前未关闭。

有种说法是,测试环境和正式环境,都属于线上的环境,所以出现这种错误。对此不是很清楚。 我认为正式环境和测试环境是相对独立开的,在不同的服务器上,应该不是测试服务器导致的这个错误。

贴个自己的 Nginx 配置,不知道有没有帮助,我目前用 WS HTTPS。

proxy_pass         http://app_backend/cable;
proxy_redirect off;
proxy_buffering   on;
proxy_set_header  X-Real-IP  $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
gzip off;

#8 楼 @hlcfan 谢谢呐。我貌似没有配置这些,直接绑定在 puma 上去跑了 cable。

之前也遇到过。nginx 配置的问题 cable 接口 Upgrade 一下就好了

#10 楼 @oyaxira 这个之前试过的,一样的错误。配置:

upstream puma {
  server unix://path/appname-puma.sock;
}

server {
  ...
  location @puma {
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "Upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;

  }
}

#11 楼 @hesongGG 缺少 proxy_http_version 1.1,这个很关键

#12 楼 @mengqing 这个也是携带了的,刚又试了下,也不可以。 个人认为,和 nginx 配置没多大的关系,毕竟测试服务器是跑起来了。 不过星期六的时候,把他换成了和 app 一起跑,是可以跑起来了。

会是防火墙么? 吃瓜群众路过,还没玩过 ActiveCable

最后怎么解决的呢?

#15 楼 @lancegin 最后是和 rails 放在一起跑的。 之前是独立部署存在的问题。

You need to Sign in before reply, if you don't have an account, please Sign up first.