2017-10-09 45 views
0

我有一個完整的生產服務器,具有以下設置: 在端口3000上使用Nginx和SSL配置的NodeJS。ExpressJS作爲前端。與A服務器(ExpressJS + Nodejs + Nginx + SSL)的套接字連接問題

現在我想添加一些套接字工作裏面。 我創建了一個名爲使用下面的代碼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的本地主機開發服務器上嘗試它時,它確實有效。

也許我不得不添加一些Nginx配置或安全連接來查看套接字,但我無法在線找到任何線索。

請任何幫助,將不勝感激!

回答

0

普通TCP套接字實際上並不需要Nginx來操作。它們也沒有加密,因此不使用SSL。您的代碼在生產環境中無法正常工作但在本地工作的最可能原因是您收聽迴環接口127.0.0.1。該接口絕不會連接到外部網絡,您無法從外部訪問它。

您應該聽取分配給服務器上某個特定接口的IP,還是聽從本質上意味着「監聽機器所有接口」的0.0.0.0