【问题标题】:SockJS + Proxy + Apache2 : wss:// not working, only pollingSockJS + Proxy + Apache2:wss:// 不工作,只能轮询
【发布时间】:2020-04-10 16:07:04
【问题描述】:

我得到了以下设置:

  1. SockJS 前端
  2. 网关服务器,将 Socket-Requests 重定向到 SockJS 节点服务器,并将其他所有内容重定向到负载平衡器

Websocket 可以正常工作,但只能用于轮询。 尝试连接到 sockJS 服务器时,我的浏览器出现以下错误:

websocket.js:6 WebSocket connection to 'wss://mypage.com/... failed: Error during WebSocket handshake: Unexpected response code: 500。 SockJS 服务器没有记录任何错误。

我可以在 devtools 中看到有一个 https://mypage.com/socket/225/ye1ifrre/xhr_streaming?... HTTP 轮询打开,但 wss:// 似乎不起作用。

这是我的网关 apache2 VHOST(我用...替换了一些内容):

<VirtualHost *:80>
    # redirect all to HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
    # WebApp VHOST

    ServerName mypage.com

    # Set HSTS
    LoadModule headers_module modules/mod_headers.so
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"


    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3
    SSLHonorCipherOrder on
    SSLCipherSuite ...
    SSLCertificateFile /...
    SSLCertificateKeyFile /...
    SSLCACertificateFile /...

    SetEnvIf Referer mypage.com localreferer
    <Location /api>
       Order deny,allow
             Allow from all
    </Location>
    # Proxy All to Application Server Load Balancer except websocket
    ProxyPreserveHost On

    # Websocket Proxy Bypass
    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    RewriteRule .* ws://10.2.0.7:9000%{REQUEST_URI} [P]
    ProxyPass /socket http://10.2.0.7:9000/socket
    ProxyPassReverse /socket http://10.2.0.7:9000/socket

    ProxyRequests off

    ProxyPass / http://10.2.0.10:80/
    ProxyPassReverse / http://10.2.0.10:80/

    # Set response headers
    Header set X-Content-Type-Options "nosniff"
    Header set Content-Security-Policy "..."
        Header set Referrer-Policy "same-origin"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"

</VirtualHost>

在我的 sockJS 服务器上,我正在收听 10.2.0.7:9000。在我们的测试环境中,sockJS 服务器与网关在同一个虚拟机上运行,​​它可以工作。在那里,我将 websockets 请求代理到 127.0.0.1:9000,并且 sockJS 服务器也侦听 127.0.0.1:9000

我正在使用 SockJS 客户端 1.4.0 和 SockJS 节点服务器 0.3.20

我在这里缺少什么?我的 CSP 标头可能有问题吗?我想我应该会收到来自 Chrome 的相关错误。

我读到这可能是 sockJS 没有监听 127.0.0.1 的问题,或者来自客户端的套接字请求必须针对 IP 地址而不是域进行。其他人有/遇到过同样的问题吗?

【问题讨论】:

    标签: websocket proxy apache2 reverse-proxy sockjs


    【解决方案1】:

    查看我的 apache 错误日志后,我看到了消息 No protocol handler was valid for the URL /socket/316/i0hkgyrq/websocket. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

    原来我必须通过运行命令 sudo a2enmod proxy_wstunnel 来启用代理 webtunnel mod。现在一切正常,我在 WS-Tab 下的 devtools 中看到了 websocket 连接

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 2016-09-07
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      相关资源
      最近更新 更多