简体   繁体   English

如何在node express socket.io下设置SSL?

[英]How to setup SSL under node express socket.io?

I basically have node.js code that start off like this: 我基本上有像这样开始的node.js代码:

var app = require('express')();
var http = require('http').createServer(app).listen(5051, function() {
    console.log('listening on 127.0.0.1:5051');
});
var io = require('socket.io')(http);
var lobby = io.of('/lobby');

lobby.on("connection", function (socket) {
    console.log("Successful connection lobby");
});

So I start my server, I start my app, get a connection, no problem. 所以我启动服务器,启动应用程序,建立连接,没问题。 Trying to add SSL however is not working. 但是,尝试添加SSL无效。 I followed the certificate creating instructions from this website using the internal address as the common name. 我遵循了使用内部地址作为通用名称从该网站创建证书的说明。

var app = require('express')();
var fs = require('fs');
var options = {
    key     : fs.readFileSync('/"path_to"/hacksparrow-key.pem'),
    cert    : fs.readFileSync('/"path_to"/hacksparrow-cert.pem') 
};
var https = require('https').createServer(options,app).listen(5051, function() {
    console.log('listening on 127.0.0.1:5051');
});
var io = require('socket.io')(https);
var lobby = io.of('/lobby');
lobby.on("connection", function (socket) {
        console.log("Successful connection lobby");
    });

Made sure my client used https instead of http, no connection, nothing. 确保我的客户端使用https而不是http,没有连接,什么也没有。 Anyone have any idea why this is happening, also, if I were using something like Nginx and used ssl on it, would this step even be necessary? 任何人都知道为什么会这样,而且,如果我使用的是Nginx之类的东西,并且在上面使用了ssl,是否还需要执行此步骤?

It sounds like the problem is that nginx is sending an http requests to your node https server. 听起来问题在于nginx正在将HTTP请求发送到您的节点https服务器。

If nginx was set up to use ssl then it would not be necessary for node to use ssl. 如果将nginx设置为使用ssl,则节点不必使用ssl。 Both servers could use ssl if you want them to. 如果需要,两台服务器都可以使用ssl。

Your certificate will generally be generated with a passphrase. 您的证书通常会使用密码生成。 This is required to initiate the SSL. 这是启动SSL所必需的。

So when you start the app, node.js will prompt for it once. 因此,当您启动应用程序时,node.js将提示您一次。 You can bypass that by adding "passphrase" key in your variable "options" 您可以通过在变量“选项”中添加“密码”键来绕开该密码

var options = {
    key     : fs.readFileSync('/"path_to"/hacksparrow-key.pem'),
    cert    : fs.readFileSync('/"path_to"/hacksparrow-cert.pem'),
    passphrase: type_your_certificate's_passphrase
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM