【问题标题】:Socket connection issue with A server (ExpressJS + Nodejs + Nginx + SSL)A服务器的套接字连接问题(ExpressJS + Nodejs + Nginx + SSL)
【发布时间】:2018-03-20 15:23:21
【问题描述】:

我有一个完全正常工作的生产服务器,其设置如下: NodeJS 在 3000 端口上配置了 Nginx 和 SSL。ExpressJS 在前面。

现在我正在尝试在里面添加一些套接字工作。 我使用以下代码创建了一个名为 server.js 的新文件:

var net = require('net');

var HOST = '127.0.0.1';
var PORT = 6969;

net.createServer(function(sock) {

// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {

    console.log('DATA ' + sock.remoteAddress + ': ' + data);
    // Write the data back to the socket, the client will receive it as data from the server
    sock.write('You said "' + data + '"');

});

// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
    console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});

sock.on('error', function(err) {
  console.log(err)
})

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

我在我的主 app.js 文件中添加了这一行:

require('./app/server');

现在,当我启动服务器时,它会按预期打印以下行: “套接字服务器正在侦听 127.0.0.1:6969” 和 "NodeJS 生产服务器监听端口 3000"

现在,当我尝试通过以下方式访问套接字服务器时:

netcat [destination] 6969

它不起作用。 但是当我在没有 SSL 和 Nginx 的 localhost 开发服务器上尝试它时,它确实有效。

可能我必须添加一些 Nginx 配置或安全连接才能看到套接字,但我在网上找不到任何线索。

任何帮助将不胜感激!

【问题讨论】:

    标签: node.js sockets ssl nginx


    【解决方案1】:

    普通的 TCP 套接字实际上并不需要 Nginx 来操作。它们也没有加密,因此不使用 SSL。您的代码无法在生产环境中运行但在本地运行的最可能原因是您收听了环回接口127.0.0.1。这个接口绝不是连接到外部网络,你无法从外部访问它。

    您应该监听分配给服务器上特定接口的 IP,还是分配给0.0.0.0,这实质上意味着“监听机器的所有接口”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2016-12-21
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多