当我启用https网站时停止工作

时间:2015-10-14 17:41:40

标签: node.js ubuntu ssl https ubuntu-14.04

我是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网站的情况下运行网站工作正常

有什么想法吗?

由于

2 个答案:

答案 0 :(得分:1)

您应该将sslOption json的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');
  });

希望它有所帮助!

答案 1 :(得分:0)

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