简体   繁体   English

我应该将密钥和证书存储在https服务器(Node.js)的哪里

[英]Where am I supposed to store my key and cert for an https server (Node.js)

I'm very new to ssl and am having trouble setting up my node project with https. 我是ssl的新手,无法使用https设置节点项目。 I've signed up and got a free certificate from StartSSL. 我已经注册并从StartSSL获得了免费证书。 I've got my private key in a file called key.pem. 我的私钥存在于名为key.pem的文件中。 I've validated my domain name and have my certificate in a cert.pem file. 我已经验证了域名,并将我的证书保存在cert.pem文件中。

Here's how I had my server set up on HTTP: 这是我在HTTP上设置服务器的方式:

    var server = require('http').createServer(app);
    var port = process.env.PORT || 3000;
    server.listen(port, function () {
      console.log('Server listening at port %d', port);
    });

This works fine, all my pages loaded accordingly. 这可以正常工作,相应地加载了我的所有页面。 Here's what how I'm trying to set up my https server: 这是我尝试设置https服务器的方式:

var https = require("https");
var fs = require("fs");
var key_file   = "key.pem";
var cert_file  = "cert.pem";
var passphrase = "...";
var config     = {
  key: fs.readFileSync(key_file),
  cert: fs.readFileSync(cert_file),
  passphrase: passphrase
};
var server = https.createServer(config,app).listen(443);

The problem is that localhost won't load anything. 问题在于本地主机不会加载任何内容。 I don't get any errors in my console, which is annoying because I have no clue what I'm doing wrong. 我的控制台没有出现任何错误,这很烦人,因为我不知道自己在做什么错。 As you can see I'm just putting my key and cert in the root of my project, which I'm not sure if I'm supposed to do or it. 如您所见,我只是将密钥和证书放在项目的根目录中,我不确定是否应该这样做。 Shouldn't I just be able to got to localhost:443 or https://localhost:443 and see my website? 我是否应该能够访问localhost:443或https:// localhost:443并查看我的网站? I've tried to follow like 6 tutorials now and can't seem to get anything to work. 我现在尝试按照6个教程进行学习,似乎无法解决任何问题。 Am I even doing this right? 我什至这样做对吗? Because I'm not getting any certificate from the client which I thought I was supposed to do. 因为我没有从客户那里得到我认为应该做的任何证书。

Tried this as well which also didn't work: 也尝试过这也没有用:

var https = require("https");
var constants = require("constants");
var fs = require("fs");
 var passphrase = "...";
https.createServer({
    key: fs.readFileSync("key.pem"),
    cert: fs.readFileSync("cert.pem"),
    passphrase: passphrase,
    secureProtocol: 'SSLv23_method',
    honorCipherOrder: true,
    secureOptions: constants.SSL_OP_NO_SSLv3 | constants.SSL_OP_NO_SSLv2
}, function(req, res) {
    req.end("Hello, SSL World!");
}).listen(443, function() {
    console.log("SSL Proxy listening on port 443");
});

Can't get the hello world even though it says it's listening on 443 in the console. 即使它说正在控制台中监听443,也无法获得世界。


EDIT: This is my entire index.js and I still can't get the hello world. 编辑:这是我整个index.js,我仍然无法获得世界。 All the dependencies are installed. 所有依赖项均已安装。 What could be the issue? 可能是什么问题?

var express = require('express');
var app = express();
var https = require("https");
var fs = require("fs");
var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};
https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

I have tried your code and it works. 我已经尝试过您的代码,并且可以正常工作。 But you need to change 但是你需要改变

req.end("Hello, SSL World!");

to

res.end("Hello, SSL World!");

Otherwise try to follow this example that is even simplier https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener 否则,请尝试遵循此示例,该示例甚至更简单https://nodejs.org/api/https.html#https_https_createserver_options_requestlistener

Be sure to launch the server as root, as you are using the port 443 请确保以root用户身份启动服务器,因为您正在使用端口443。

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

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