bg-tutorials

如何在 Node.js 上安装 SSL 证书

在本教程中,您将学习如何使用内置的 https 模块在 Node.js 上安装 SSL 证书。与 Apache 或 Nginx 等 Web 服务器不同,Node.js 是直接在 JavaScript 中加载您的证书、私钥和 CA 证书链,因此”安装”实际上是在您的应用程序代码内部完成的。

在 Node.js 中生成 CSR 代码

如果您还没有申请 SSL 证书,首先需要生成 CSR(证书签名请求)。CSR 是一个包含您联系方式和公钥的小型编码文本块,您在下单时需要将其提交给证书颁发机构(CA)。您有两种选择:

在下单时将 CSR 提交给证书颁发机构。待 CA 完成验证并颁发您的 SSL 证书后,继续进行下面的安装步骤。

在 Node.js 上安装 SSL 证书

第 1 步:准备证书文件

验证完成后,证书颁发机构会通过电子邮件将证书文件发送给您。解压该文件包。您应该会得到:

  • yourdomain.crt,您的主域名证书。
  • yourdomain.ca-bundle,构成CA 证书包的中间证书(及根证书)。
  • yourdomain.key,您在生成 CSR 时同时生成的私钥。

浏览器必须接收到中间证书链,而不仅仅是您的域名证书,否则某些客户端会拒绝该连接。在 Node.js 中提供证书链的最简洁方法是将您的证书和 CA 证书包合并为一个完整证书链(full-chain)文件:

cat yourdomain.crt yourdomain.ca-bundle > fullchain.crt

将 fullchain.crt 和 yourdomain.key 文件放置在您的应用可以读取的位置,并确保私钥文件只能被运行 Node.js 的用户读取。

第 2 步:在 Node.js 中创建 HTTPS 服务器

创建一个服务器文件。在本示例中,我们将其命名为 https_server.js,但您可以使用任何文件名。下面的示例使用 CommonJS(require),在所有受支持的 Node.js 版本中均可使用。请将粗体部分的路径替换为您自己的路径:

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('/path/to/private.key'),
  cert: fs.readFileSync('/path/to/fullchain.crt'),
  // Recommended TLS hardening (2026): never negotiate below TLS 1.2
  minVersion: 'TLSv1.2'
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('Welcome to a Node.js HTTPS servern');
}).listen(443);

您需要将粗体部分替换为您自己的信息:

  • /path/to/private.key:您私钥文件的完整路径。
  • /path/to/fullchain.crt:您在第 1 步中创建的完整证书链文件的完整路径。

关于端口的说明: HTTPS 默认使用端口 443。绑定该端口需要提升的系统权限,因此在反向代理后面或开发环境中,您可能更倾向于使用一个高位端口,例如 8443。只需更改传递给 listen() 的值即可。

更喜欢使用 ES 模块? 如果您的项目在 package.json 中使用了 "type": "module"(或使用 .mjs 文件),请改用 import 语法。TLS 选项是相同的:

import https from 'node:https';
import fs from 'node:fs';

const options = {
  key: fs.readFileSync('/path/to/private.key'),
  cert: fs.readFileSync('/path/to/fullchain.crt'),
  minVersion: 'TLSv1.2'
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('Welcome to a Node.js HTTPS servern');
}).listen(443);

替代方案:使用 ca 选项传递证书链。 如果您希望将文件分开保存,而不是构建完整证书链文件,可以让 cert 指向您的域名证书,并通过 ca 数组提供中间证书:

const options = {
  key: fs.readFileSync('/path/to/private.key'),
  cert: fs.readFileSync('/path/to/yourdomain.crt'),
  ca: [
    fs.readFileSync('/path/to/intermediate.crt'),
    fs.readFileSync('/path/to/ca_bundle.crt')
  ],
  minVersion: 'TLSv1.2'
};

使用 Express? 将您的 Express app 作为请求处理器传入。证书选项保持完全相同:

const express = require('express');
const https = require('https');
const fs = require('fs');

const app = express();
app.get('/', (req, res) => res.send('Hello over HTTPS'));

const options = {
  key: fs.readFileSync('/path/to/private.key'),
  cert: fs.readFileSync('/path/to/fullchain.crt'),
  minVersion: 'TLSv1.2'
};

https.createServer(options, app).listen(443);

第 3 步:启动您的 Node.js HTTPS 服务器

运行以下命令来启动应用程序:

node https_server.js

现在,您的应用程序已通过 HTTPS 提供服务。对于生产环境部署,建议在进程管理器(例如 PM2 或 systemd 服务)下运行它,以便自动重启并在系统重启后仍能正常运行。

第 4 步:测试您的 SSL 安装

使用我们的SSL 检测工具之一,测试您的 SSL 安装是否存在潜在错误或漏洞。您也可以通过命令行确认证书链:

echo | openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 2>/dev/null | openssl x509 -noout -issuer -dates

此命令会打印证书的颁发者和有效期,确认您的 Node.js 服务器所展示的证书是正确的。

在哪里可以购买适用于 Node.js 的 SSL 证书?

SSL Dragon 是您满足所有 SSL 需求的来源。我们提供市场上极具竞争力的价格,涵盖我们全部的 SSL 产品系列,并与行业内最优秀的 SSL 品牌建立了合作关系,以提供强大的安全保障和专业的支持服务。我们所有的 SSL 证书均与 Node.js 应用程序完全兼容。

快速颁发、强大加密、99.99% 的浏览器信任度、专属支持,以及 25 天退款保证。

常见问题

如何在 Node.js 中启用 HTTPS?

使用内置的 https 模块。使用 fs.readFileSync() 读取您的私钥和完整证书链文件,将它们作为 key 和 cert 选项传递给 https.createServer(),然后调用 .listen(443)。Node.js 会为您处理 TLS 握手过程;无需配置单独的 Web 服务器。

在 Node.js 中我需要使用 ca 选项吗?

您需要提供中间证书链,但并不一定需要使用 ca 选项来实现这一点。最简单的方法是将您的域名证书和 CA 证书包合并成一个完整证书链文件,并让 cert 指向该文件。只有当您希望将证书和中间证书保存在不同文件中时,才需要使用 ca 数组。

为什么我的 Node.js 服务器无法绑定到端口 443?

在 Linux 和 macOS 系统中,1024 以下的端口属于特权端口。直接绑定到端口 443 需要提升的系统权限,这就是为什么许多配置会让 Node.js 运行在高位端口(例如 8443)上,并放在负责终止或转发 TLS 的反向代理(Nginx 或负载均衡器)之后。在开发环境中,只需更改传递给 listen() 的值即可。

我可以使用 import 而不是 require 来创建 HTTPS 服务器吗?

可以。CommonJS(require)在所有受支持的 Node.js 版本中均可使用,但如果您的项目配置为 ES 模块(在 package.json 中设置了 “type”: “module”,或使用 .mjs 文件),您可以改用 import https from ‘node:https’。无论采用哪种方式,TLS 选项都是相同的。

如何检查 SSL 证书在 Node.js 上是否正常工作?

在浏览器中打开您的网站,查看是否显示锁形图标,或使用我们的SSL 检测工具进行检测,也可以在终端中运行 echo | openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 命令来检查服务器所呈现的证书。

总结

在 Node.js 上安装 SSL 证书,归根结底就是:构建一个完整证书链文件,使用 fs.readFileSync() 读取它以及您的私钥,将它们与 minVersion: ‘TLSv1.2’ 一起传递给 https.createServer(),然后通过 node https_server.js 启动应用程序。

还没有证书?浏览我们的SSL 证书产品。

立即订购 SSL 证书, 可节省 10% 的费用!

快速发行, 强大加密, 99.99% 的浏览器信任度, 专业支持和 25 天退款保证. 优惠券代码 SAVE10

龙飞行的详细图像
撰写人

I've been building and managing websites for over 20 years, with a heavy focus on the technical side of the cybersecurity, VPN, and SaaS industries. I know how sites are built from the ground up, which means I know how to secure them. Here at SSL Dragon, I write about web architecture, encryption, and keeping your infrastructure safe.

Avatar of Sergiu Rosca
Technical Review by Sergiu Rosca

Sergiu Rosca is the core web developer behind SSL Dragon. He manages the technical infrastructure, platform performance, and backend integrations that keep the site running smoothly and securely. At SSL Dragon, Sergiu shares practical insights on web development, site optimization, and technical troubleshooting.

All SSL Dragon installation guides are tested on live server environments and undergo a strict peer-review process to ensure your infrastructure remains secure. Read our full Editorial Policy.