【问题标题】:WebSocket Error in connection establishment: net::ERR_CONNECTION_CLOSEDWebSocket 连接建立错误:net::ERR_CONNECTION_CLOSED
【发布时间】:2020-01-24 09:24:43
【问题描述】:

当我尝试与我的服务器建立wss 连接时出现此错误:

到“wss://mydomain:3000/”的 WebSocket 连接失败:错误 连接建立:net::ERR_CONNECTION_CLOSED

我目前有一个 apache2 虚拟主机配置设置来监听端口 443 和 80 上的请求:

<VirtualHost *:80>
        ServerName otherdomainname.co.uk
        ServerAlias www.otherdomainname.co.uk

        RewriteEngine On
        RewriteRule ^/(.*)$ /app/$1 [l,PT]

        JkMount /* worker2

</VirtualHost>

<VirtualHost _default_:443>
        ServerName otherdomainname.co.uk
        ServerAlias www.otherdomainname.co.uk

        RewriteEngine On
        RewriteRule ^/(.*)$ /app/$1 [l,PT]

        SSLEngine On
        SSLCertificateFile /etc/apache2/ssl/apache.crt
        SSLCertificateKeyFile /etc/apache2/ssl/apache.key

        <Location />
        SSLRequireSSL On
        SSLVerifyClient optional
        SSLVerifyDepth 1
        SSLOptions +StdEnvVars +StrictRequire
        </Location>

        JkMount /* worker2

</VirtualHost>

如您所见,它使用 JkMount 将请求传递给 Tomcat,后者在 HTTP 和 HTTPS 上都正确地为网页提供服务。

当我在端口 80 上使用 HTTP 协议访问该站点时,可以使用 ws 协议建立 WebSocket 连接。

当我在端口 443 上使用 HTTPS 协议访问站点时,站点服务正常,但没有使用 wss 建立 WebSocket 连接。

我正在使用“ws”node.js 模块来提供 WebSocket 服务器:

var WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({ port: 3000 }),
  fs = require('fs');

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);

    ws.send(message);
  ws.send('something');
});

为什么我无法通过https 使用wss 协议成功连接到WebSocket 服务器?

【问题讨论】:

    标签: node.js apache https websocket wss


    【解决方案1】:

    问题是我没有为 https/wss 配置 WebSocket 服务器。

    这是我使用 node.js 中的“ws”的不安全 WebSocket 服务器的安全版本。

    var WebSocketServer = require('ws').Server,
      fs = require('fs');
    
    
    var cfg = {
            ssl: true,
            port: 3000,
            ssl_key: '/path/to/apache.key',
            ssl_cert: '/path/to/apache.crt'
        };
    
    var httpServ = ( cfg.ssl ) ? require('https') : require('http');
    
    var app      = null;
    
    var processRequest = function( req, res ) {
    
        res.writeHead(200);
        res.end("All glory to WebSockets!\n");
    };
    
    if ( cfg.ssl ) {
    
        app = httpServ.createServer({
    
            // providing server with  SSL key/cert
            key: fs.readFileSync( cfg.ssl_key ),
            cert: fs.readFileSync( cfg.ssl_cert )
    
        }, processRequest ).listen( cfg.port );
    
    } else {
    
        app = httpServ.createServer( processRequest ).listen( cfg.port );
    }
    
    var wss = new WebSocketServer( { server: app } );
    
    wss.on('connection', function connection(ws) {
      ws.on('message', function incoming(message) {
        console.log('received: %s', message);
    
        ws.send(message);
    
      });
    
      ws.send('something');
    });
    

    【讨论】:

    • 我在哪里配置这个?我是 Node.JS 的新手,遇到了这个问题。
    • 您必须设置您的网络服务器以进行公钥加密,并提供密钥和证书信息。 chrismepham.co.uk/blog/guide/…
    【解决方案2】:

    我遇到了类似的问题,原来我使用的是 CloudFlare,它只允许非常特定的端口通过。

    所以在 3000 端口上运行的流星立即被阻塞了。

    重新配置我的反向代理设置并在允许的端口上运行 Meteor 解决了我的问题。

    但是,最后,我关闭了 Meteor 部署上的套接字。它似乎没有影响性能。祝你好运,

    4 年后更新哈哈

    所以我们使用 Apache2 并在端口 80 上侦听域,但在这种情况下,我们将获取端口 80 的流量并将其重定向到 localhost 端口 3020。它实际上可以是任何端口。希望这可以帮助!如果你想看的话,来看看 www.StarLordsOnline.com :)

    <VirtualHost *:80>
    ServerAdmin info@starlordsonline.com
    ServerName starlordsonline.com
    ServerAlias www.starlordsonline.com
    
    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    RewriteRule .* ws://localhost:3020%{REQUEST_URI} [P]
    
    ProxyRequests off
    
    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>
    
    <Location />
            ProxyPass http://localhost:3020/
            ProxyPassReverse http://localhost:3020/
    </Location>
    

    【讨论】:

    • 非常感谢,我花了一整天的时间来解决这个问题。谢谢
    • 很高兴它有效。发布后 4 年大声笑。我将用我的虚拟主机示例更新我的答案。
    猜你喜欢
    • 2016-03-11
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2019-08-30
    • 2014-12-19
    相关资源
    最近更新 更多