根据论坛里的华顺哥的两篇 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;
}
}