Rails ActionCable 单独部署不行?

xiaoxiao · 2017年09月19日 · 最后由 xiaoxiao 回复于 2017年11月05日 · 1838 次阅读

根据论坛里的华顺哥的两篇 actioncable 部署文章及网上相关资料,部署了 nginx+puma+actioncable 方式,具体配置如下:

旧的nginx配置如下:
server {
    listen 80;
    client_max_body_size 100m;
    server_name example.com;
    access_log /var/log/nginx/example.com.access.log;
    location / {
       proxy_pass          http://tagsystem_servers;
    }   
   location /cable {
       proxy_pass           http://127.0.0.1:28080/;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";
   }
   upstream tagsystem_servers{
       server 127.0.0.1:3001;
   }   
}

分别运行 rails s 和 actioncable 服务器命令 puma -p 28080 cable/config.ru,发现websocket功能不能正常使用,相关代码

App.message = App.cable.subscriptions.create "MessageChannel",
  connected: ->
    console.log("connect")
    # Called when the subscription is ready for use on the server

  disconnected: ->
    console.log("disconnect")
    # Called when the subscription has been terminated by the server

  received: (data) ->
    alert("data")
    # Called when there's incoming data on the websocket for this channel

在 chrome 调试器下观察是 connect 能打印出来,但是没法 alert 数据,然后后来查找了很久,发现我这边 nginx 配置成如下,其他代码都没有动,发现能正常运行,alert 也能出来,相当于 actioncable 不单独部署,真是诡异的信息,哪位大神能解释一下

新的nginx配置如下:
server {
    listen 80;
    client_max_body_size 100m;
    server_name example.com;
    access_log /var/log/nginx/example.com.access.log;
    location / {
       proxy_pass          http://tagsystem_servers;
    }   
   location /cable {
       proxy_pass          http://tagsystem_servers;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";
   }
   upstream tagsystem_servers{
      server 127.0.0.1:3001;
   }   
}

什么意思

xiaoxiao 关闭了讨论。 11月05日 21:01
xiaoxiao 重新开启了讨论。 11月05日 21:01

这几天调试成功了

xiaoxiao 关闭了讨论。 11月05日 21:01
需要 登录 后方可回复, 如果你还没有账号请 注册新账号