SockJS +代理+ Apache2:wss://不起作用,仅轮询

时间:2020-04-10 16:07:04

标签: websocket proxy apache2 reverse-proxy sockjs

我进行了以下设置:

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

Websockets可以正常工作,但只能用于轮询。 尝试连接到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服务器与网关在同一VM上运行,它可以工作。我在那里将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地址而不是域。其他人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:0)

查看了我的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连接。