node.js - 使用 .key 和 .crt 文件的 GoDaddy 证书在 Node 中使用 SSL

标签 node.js ssl

<分区>

我一直在研究在我的 Node 服务器中使用 ssl 证书的解决方案。有不同的实现可用,但看不到带有 .key 文件和 .crt 文件的 godaddy 证书的具体示例。 有帮助吗?

最佳答案

您需要遵循几个步骤

1- 生成 csr 文件。使用以下命令

openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -out yourdomain.csr
  1. key 用作 ssl 安全证书 key 。您不需要与任何人共享此文件。
  2. csr用于从godaddy生成证书

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/

相关文章:

node.js - Nodejs中stdin的结束进程

node.js - 如何使用Nodejs将wav文件转换为8000hz

node.js - Twitter API 问题(代码 : 349, 消息 : 'You cannot send messages to this user.' ) in node. js

email - Cakephp:电子邮件错误 SSL

.net - 验证 .NET 套接字上使用的 SSL/TLS 级别?

java - 自签名证书旁路不起作用

node.js - fastify中如何组织路由?

javascript - 在 Node 应用程序中多次初始化快速功能是否会影响其性能

ubuntu - 将 NGINX http 重定向到 https

ssl - 只有 TLS_RSA_WITH_AES_256_CBC_SHA 密码套件的自签名 SSL 证书