Node.js https pem错误:错误:0906D06C:PEM例程:PEM_read_bio:无起始行

我从证书颁发机构获得了这个文件:

  • domain.com.p7b
  • domain.com.crt
  • domain.com.ca束

我试过这个小代码:

var express = require('express'); var app = express(); var fs = require("fs"); var https = require('https'); var privateKey = fs.readFileSync('domain.com.p7b').toString(); var certificate = fs.readFileSync('domain.com.crt').toString(); var ca_bundle = fs.readFileSync('domain.com.ca-bundle').toString(); var credentials = { key: privateKey, ca : ca_bundle, cert: certificate}; https.createServer(credentials,app).listen(8080, function () { console.log('Example app listening on port 8080!'); }); 

启动脚本后,我得到以下错误:

 (err): at Object.createSecureContext (_tls_common.js:87:19) (err): at Server (_tls_wrap.js:721:25) (err): at new Server (https.js:17:14) (err): at Object.exports.createServer (https.js:37:10) (err): at Object.<anonymous> (/utec_temp/https/web.js:27:7) (err): at Module._compile (module.js:435:26) (err): at Object.Module._extensions..js (module.js:442:10) (err): at Module.load (module.js:356:32) (err): at Function.Module._load (module.js:311:12) (err): Error: error:0906D06C:PEM routines:PEM_read_bio:no start line (err): at Error (native) (err): at Object.createSecureContext (_tls_common.js:87:19) (err): at Server (_tls_wrap.js:721:25) (err): at new Server (https.js:17:14) (err): at Object.exports.createServer (https.js:37:10) (err): at Object.<anonymous> (/utec_temp/https/web.js:27:7) (err): at Module._compile (module.js:435:26) (err): at Object.Module._extensions..js (module.js:442:10) (err): at Module.load (module.js:356:32) (err): at Function.Module._load (module.js:311:12) 

你可以在互联网上谷歌的大多数例子使用自签名证书,但是当我在真实环境中工作时发生了什么?

我的小代码在自签名密钥的开发中工作,如下例所示:

https://stackoverflow.com/a/24283204/3957754

我研究了,我发现这个:

https://www.namecheap.com/support/knowledgebase/article.aspx/9705/0/nodejs

http://www.backwardcompatible.net/155-Setting-up-real-SSL-Nodejs-Express

Node.js https pem错误:例程:PEM_read_bio:没有开始行

但我不能纠正错误。

我也减less到一个文件:

 var credentials = {cert: certificate}; 

而错误是一样的。 所以我想,也许从Windows到UNIX的格式错误。 我用dos2unix工具,错误是一样的。

我的节点版本是4.4.7

任何帮助表示赞赏。

提前致谢!

更新

我在答案部分答案适用于node.js! 但在一个优雅或健康的方式, 不要修改您的APP代码,并离开这个工作到Apache,Nginx,haproxy或一些负载平衡器:

 # apache 2.2 example SSLCertificateFile /some/folder/certificate.crt SSLCertificateKeyFile /some/folder/initial.key SSLCertificateChainFile /some/folder/certificate.ca-bundle 

这种复杂性对于开发团队来说必须是透明的,并且应该由系统pipe理员,基础设施或与贵公司networking相关的其他团队进行pipe理。

我有点晚,但我希望这有助于。

如果有人使用这个文件:pb7,crt,ca-bundle并且有这个错误:

 error:0906D06C:PEM routines:PEM_read_bio:no start line 

这将意味着这个文件是错误的,损坏或被要求为另一个环境(例如窗口),因为这篇文章说: https : //serverfault.com/a/317038

所以在我的情况下,解决scheme是要求新的证书,并在规范中,提出以下内容:

  • Linux兼容性

保存csr创build并发送给authentication者提供者(我称为initial.key )的密钥也很重要。

示例http://www.backwardcompatible.net/155-Setting-up-real-SSL-Nodejs-Express

最后,您的提供商会向您发送一个包含多个文件的zip文件。 您只需要一个.crt文件为您的节点应用程序:

 var privateKey = fs.readFileSync('/some/folder/initial.key').toString(); var certificate = fs.readFileSync('/some/folder/certificate.crt').toString(); var credentials = {key: privateKey, cert: certificate}; 

注意:certificate.ca-bundle和certificate.crt文件必须由authentication者提供者发送。

HTH