当我启用 https 网站停止工作

When i enabled https sites stop working

我是 nodejs 的新手,我已经在 nodejs 上安装了 ubuntu 我也有 comodo 的 SSL 证书

我按照以下步骤操作

1) Login to root account and copy the key file in SSL cert to  /etc/ssl/private/private.key file.
2) Then created file called /etc/ssl/certs/STAR_cert.com.crt and paste  the contents of STAR_cert_com.crt into it.

3) Then create file called /etc/ssl/certs/AddTrustExternalCARoot.crt file and paste all the contents of three files present inside "CA and Intermediate Certs" folder.

然后我将 app.js 文件配置为以下

var sslOptions = {
  key: fs.readFileSync('/etc/ssl/private/private.key'),
  cert: fs.readFileSync('/etc/ssl/certs/STAR_cert_com.crt'),
  requestCert: false,
  //ca: fs.readFileSync('/etc/ssl/certs/AddTrustExternalCARoot.crt'),
  rejectUnauthorized: false 
};

var secureServer = https.createServer(sslOptions,app).listen(443, function(){
   console.log("Express server listening on port : " + app.get('port'));
    console.log("Mode : " + app.get('mode'));
});

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
    res.end();
}).listen(80);
var secureServer = https.createServer(sslOptions,app).listen(443, function(){
   console.log("Express server listening on port : " + app.get('port'));
    console.log("Mode : " + app.get('mode'));
});

当我 运行 我的网站 example.com 它重定向到 https://example.com 并给我以下错误

>     This webpage is not available
>     
>     DNS_PROBE_FINISHED_NXDOMAIN

但服务器控制台上没有错误。

如果我 运行 没有 https 的网站工作正常

有什么想法吗?

谢谢

您应该将 sslOption json 的密钥和证书参数作为字符串传递,例如,您可以使用 express 4+ 轻松启动 https nodejs 服务器,例如:

 var fs = require('fs');
 var options = {
    key: fs.readFileSync('your.key').toString(),
    cert: fs.readFileSync('your_crt.crt').toString()
  };
  var https = require('https').Server(options,app);
  https.listen(config.port, function() {
    console.log('Server started successfully');
  });

希望对您有所帮助!

您似乎添加了密钥错误的证书。 仔细检查文件及其密钥对。如果您有中间证书,也包括在内。