当我启用https网站停止工作



我是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网站工作良好

你知道吗?

谢谢

您应该将sslotionjson的key和cert参数作为字符串传递,例如,您可以使用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');
  });

希望有帮助!

似乎您添加了错误密钥的证书。再次检查文件及其密钥对。如果你有中级证书,也要写进去。

最新更新