【发布时间】:2016-07-06 01:14:25
【问题描述】:
我从证书颁发机构获得了这些文件:
- domain.com.p7b
- domain.com.crt
- domain.com.ca-bundle
我尝试了这个小代码:
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)
google 上所有的例子都使用自签名证书,但是当我需要在真实环境中工作时会发生什么?
我的小代码在开发中使用自签名密钥,遵循以下示例:
我研究了一下,发现了这个:
- 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 error: routines:PEM_read_bio:no start line
但我无法纠正错误。
我也减少到一个文件:
var credentials = {cert: certificate};
而且错误是一样的。所以我认为当我将这些证书从 Windows 移动到 unix 时,可能是格式错误。我用了dos2unix工具,还是一样的错误。
我的节点版本是4.4.7
感谢任何帮助。
提前致谢!
已更新
当您使用 https 证书、域或子域时,忘记了用于开发应用程序的技术。
Node.js、java、python 和其他语言都有库可以使用 https 发布安全端点。这是通过手动加载您购买的或自签名的证书来实现的。 这行得通,但这不是正确的方法,因为。
例如:开发团队启动应用程序会有问题,因为源代码需要证书和其他配置。测试部署需要特定的证书等
为了一个干净、可维护和可扩展的架构,并遵循模式separation of concerns (SoC) 不要修改您的源代码,并将这项工作或复杂性留给 apache、nginx、haproxy、aws elb 或一些负载均衡器和路由器:
apache 2.2 示例
SSLCertificateFile /some/folder/certificate.crt
SSLCertificateKeyFile /some/folder/initial.key
SSLCertificateChainFile /some/folder/certificate.ca-bundle
nginx 示例
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/your_domain_name.pem; (or bundle.crt)
ssl_certificate_key /etc/ssl/your_domain_name.key;
server_name your.domain.com;
...
}
这种复杂性必须对开发团队透明,并应由系统管理员、基础设施或与贵公司网络相关的其他团队管理。
【问题讨论】:
标签: node.js ssl express https ssl-certificate