通过websocket与Nginx SSL终止代理连接两个节点应用

时间:2018-10-26 23:38:38

标签: node.js ssl nginx websocket

想要主机A上的节点应用程序使用WebSocket与主机B上的另一个节点应用程序通信(不涉及Web应用程序)。主机B位于SSL配置的反向代理(nginx)后面。

下面是我的配置。我正在将'ws'模块用于websockets。

我使用主机A的curl成功建立了连接。在主机A上运行节点应用程序时,它返回错误。因此,目前我假设我的nginx.conf和主机B上的节点应用程序正常,问题是主机A上的节点应用程序客户端。

如何使其工作?

节点服务器应用

var web = (function() {
    var express = require('express');
    return {
        app: express(),
        express: express
    };
})();

var server = web.app.listen(3000, function(){
  console.log('Example app listening on port 3000!');
});

const wss = new SocketServer({ server });
wss.on('connection', function connection(ws) {
    console.log("connection ...");
    ws.on('message', function incoming(message) {
        console.log('received: %s', message);
        connectedUsers.push(message);
    });
    ws.send('message from server at: ' + new Date());
});

nginx代理

server {
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl http2 default_server;
    server_name  any.com;
    ssl_certificate "/etc/pki/nginx/ssl/cert/SSL_Certificate.crt";
    ssl_certificate_key "/etc/pki/nginx/ssl/private/any.com.key";
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    location /app/wss/ {
        proxy_pass                          http://localhost:3000/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade websocket;
        proxy_set_header Connection upgrade;
    }
}

节点客户端应用

const WebSocket = require('ws');
wsClient = new WebSocket('wss://any.com/app/wss');
wsClient.on('open', function open() {
    console.log('open connection, sending message to server...');
    wsClient.send('something from client');
});
wsClient.on('message', function incoming(data) {
    console.log('clientreceived message from server:'+data);
});

节点客户端应用错误

node app
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Unexpected server response: 301
    at ClientRequest.req.on (/home/xxx/projects/app/node_modules/ws/lib/websocket.js:542:5)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:474:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at TLSSocket.socketOnData (_http_client.js:363:20)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)

成功的卷曲和反应

curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Host: localhost:3000" 
    --header "Origin: http://example.com:80" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" 
    --header "Sec-WebSocket-Version: 13" https://any.com/app/wss/

HTTP/1.1 101 Switching Protocols
Server: nginx/1.12.1
Date: Fri, 26 Oct 2018 23:25:05 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: qGEgH3En71di5rrssAZTmtRTyFk=

�?message from server at: Fri Oct 26 2018 23:25:05 GMT+0000 (UTC)^C

1 个答案:

答案 0 :(得分:0)

在将/添加到可以正常工作的客户端应用程序的URL中后,我发现更改如下:

wsClient = new WebSocket('wss://any.com/app/wss');

对此:

wsClient = new WebSocket('wss://any.com/app/wss/');

不确定哪些对象对此上游。它是反向代理Nginx或服务器应用程序。当我将相同的URL复制到(缩小的)curl命令中时,它也会失败:

curl --include --no-buffer --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" --header "Sec-WebSocket-Version: 13" https://any.com/app/wss
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Sat, 27 Oct 2018 17:19:22 GMT
Content-Type: text/html
Content-Length: 185
Location: https://any.com/app/wss/
Connection: keep-alive

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>

因此,由于偶然的错误,我在curl命令中使用了与最初使前一种方法起作用的节点客户端应用程序不同的URL。