You need to enable JavaScript to run this app. Shiny-Server配合Apache部署时WebSocket在企业网络下连接失败求助
优惠活动
大模型
产品
解决方案
定价
更多

Shiny-Server配合Apache部署时WebSocket在企业网络下连接失败求助

排查与解决思路

1. 验证企业网WebSocket限制逻辑

  • 先在企业网环境下打开浏览器控制台,执行测试代码确认基础连通性:
const ws = new WebSocket('wss://srv.socialthink.net/app/test');
ws.onopen = () => console.log('WebSocket连接成功');
ws.onerror = (e) => console.error('WebSocket连接失败', e);
  • 同步测试shinyapps.io同版本应用的WebSocket连接,确认企业网是全局禁用WebSocket,还是仅拦截不符合规则的WebSocket请求。

2. 开启Shiny Server兼容传输策略

Shiny底层依赖Sockjs传输交互数据,本身支持WebSocket连接失败后自动回退到兼容传输模式,你可以先调整Shiny Server配置开启回退策略:

  • 编辑配置文件 /etc/shiny-server/shiny-server.conf,在站点配置块内添加如下规则:
# 保留WebSocket优先级,同时开启兼容传输模式
sockjs_disable_websocket off;
sockjs_transport_polling_xhr on;
sockjs_transport_streaming_xhr on;
  • 执行 systemctl restart shiny-server 重启服务生效。

3. 优化Apache反向代理配置

你当前的代理配置缺少WebSocket连接所需的头部传递规则,需补充调整:

<VirtualHost *:443>
  # 保留你原有SSL证书、站点域名相关配置
  RewriteEngine on
  # 补充WebSocket升级校验规则
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteCond %{HTTP:Connection} upgrade [NC]
  RewriteRule /app/(.*) ws://localhost:3838/$1 [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /app/(.*) http://localhost:3838/$1 [P,L]

  ProxyPass /app/ http://localhost:3838/
  ProxyPassReverse /app/ http://localhost:3838/
  # 补充请求头部传递,避免HTTPS协议识别错误
  ProxyPreserveHost On
  ProxyPassReverseCookiePath / /app/
  RequestHeader set X-Forwarded-Proto "https"
  RequestHeader set X-Forwarded-Port "443"

  <Proxy *>
    Allow from localhost
    Require all granted
  </Proxy>

  RedirectMatch permanent ^/app$ /app/
  Header edit Location ^/ /app/
  ProxyRequests Off
</VirtualHost>
  • 执行 apache2ctl configtest 验证配置无语法错误后,执行 systemctl restart apache2 重启服务。

4. 极端场景兼容方案

如果确认企业网完全禁用了WebSocket连接,可以直接强制Shiny关闭WebSocket传输,所有交互走HTTP轮询:

  • 修改 /etc/shiny-server/shiny-server.conf 配置:
sockjs_disable_websocket on;
  • 重启Shiny Server后即可兼容所有支持HTTP的网络环境,仅高并发场景下会有少量性能损耗。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 13:30:03