<分区>
我一直在研究在我的 Node 服务器中使用 ssl 证书的解决方案。有不同的实现可用,但看不到带有 .key 文件和 .crt 文件的 godaddy 证书的具体示例。 有帮助吗?
<分区>
我一直在研究在我的 Node 服务器中使用 ssl 证书的解决方案。有不同的实现可用,但看不到带有 .key 文件和 .crt 文件的 godaddy 证书的具体示例。 有帮助吗?
最佳答案
您需要遵循几个步骤
1- 生成 csr 文件。使用以下命令
openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -out yourdomain.csr
2- 登录 gGoDaddy 帐户。
3- 在文本编辑器中打开 CSR(使用 openssl 命令生成的 .csr 文件)并复制所有文本。将完整的 CSR 粘贴到您帐户中的 SSL 注册表中。
4- 验证您的域需要一天的时间。如果您使用第三方域名服务,GoDaddy 会这样做。
5- 现在,如果您从不同的服务提供商处购买了域。打开您的域服务器仪表板并更新 DNS 记录以将其指向您的服务器。 (如果您已经这样做,请跳过此步骤)
6- 访问 https://certs.godaddy.com/cert并下载您的证书。 zip 文件将包含 2 个文件。 1 个随机数 .crt 和其他名称为 cert_*.crt。
7- 打开 cert_*.crt 文件,里面会有多个证书。现在您需要将这些证书拆分成不同的文件,例如 cert_1.crt cert_2.crt 等
现在在 Node 中使用以下代码
var secureApp = require('../app'),
fs = require('fs'),
privateKey = fs.readFileSync('domain.com.key').toString(),
certificate = fs.readFileSync('domain.com.crt').toString(),
cert_g = fs.readFileSync('cert_g.crt').toString(),
cert_g1 = fs.readFileSync('cert_g1.crt').toString(),
cert_g2 = fs.readFileSync('cert_g2.crt').toString(),
https = require('https'),
http = require('http');
/**
* Get port from environment and store in Express.
*/
var securePort = normalizePort(process.env.PORT || '443');
secureApp.set('port', securePort);
/**
* Create HTTPS secure server.
*/
// secure impl
var options = {key: privateKey, cert: certificate,ca: [cert_g, cert_g1, cert_g2]};
var secureServer = https.createServer(options, secureApp);
/**
* Listen on provided port, on all network interfaces.
*/
secureServer.listen(securePort);
secureServer.on('error', onError);
secureSrver.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
享受
关于node.js - 使用 .key 和 .crt 文件的 GoDaddy 证书在 Node 中使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406699/