You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Turbo_stream_from无法连接Rails预发布环境后端的问题求助

问题:Turbo Stream会话级频道在预发布环境无法订阅和接收广播

场景说明

  • Rails 7.1.2应用,视图通过<%= turbo_stream_from session["_csrf_token"] %>订阅会话级Turbo Stream频道(以CSRF Token作为频道名实现会话隔离)
  • 控制器动作触发广播逻辑:Turbo::StreamsChannel.broadcast_replace_to session["_csrf_token"], partial: "devise/registrations/creating", target: "registration_new_full_page",用于注册成功重定向前将表单替换为加载视图

环境差异

  • 本地开发环境(http://127.0.0.1:3000):功能完全正常,加载注册页时Rails日志会输出Turbo::StreamsChannel is streaming from [CSRF Token值],页面HTML中的<turbo-cable-stream-source>标签带有connected=""属性
  • 预发布环境:无上述频道流日志,广播的更新无法被页面接收,页面无任何反应;页面HTML中的<turbo-cable-stream-source>标签缺少connected=""属性

预发布环境Nginx代理配置

map $http_host $rails {
  myapp.com 123.123.123.123;
  myapp-staging.com 234.234.234.234;
}

...

  location /cable {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Ssl on; # Optional
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Real-IP $remote_addr;

    proxy_pass http://$rails;
  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Ssl on; # Optional
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    proxy_redirect off;

    proxy_read_timeout 120;
    proxy_connect_timeout 120;
    proxy_send_timeout 120;

    proxy_pass http://$rails;
  }

已排查内容

  • 浏览器控制台无任何报错信息,Rails和Nginx日志均未记录错误
  • 尝试用静态字符串替代CSRF Token作为频道名订阅,问题依旧存在
  • 预发布环境中,浏览器每10秒向wss://myapp-staging.com/cable发送Keepalive GET请求,返回101状态码,Rails日志显示Successfully upgraded to WebSocket,本地环境无此现象

求助疑问

请问我遗漏了哪些配置或排查步骤?可能的错误点有哪些?

内容的提问来源于stack exchange,提问作者squeezeitthroughthekeyhole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:12:56