是否可以在不重新启动服务器的情况下更新 ssl 证书?

use*_*227 6 ssl-certificate node.js

我想在不重新启动的情况下更新 node.js http2 服务器上的 ssl 证书(以避免任何停机时间)。此外,我不想在这项工作中使用任何 3rd 方模块。只有纯 nodejs。是否可以?

现在当证书即将到期时,我只是重新启动脚本。

const https = require('http2');
const server = https.createSecureServer({
  ca: fs.readFileSync('chain.pem'),
  cert: fs.readFileSync('cert.pem', 'utf8'),//fullchain
  key: fs.readFileSync('privkey.pem', 'utf8'),
  allowHTTP1: true,
},
Run Code Online (Sandbox Code Playgroud)

我希望能够观察证书文件是否更新(例如使用 fs.watch()),并动态更新 http2 服务器中的证书......

use*_*227 5

正如 Jake 所提到的, setSecureContext() 发挥了神奇的作用。似乎它可以在不中断当前连接的情况下更新证书。就像是:

setTimeout(function () {server.setSecureContext({
  ca: fs.readFileSync('chain.pem'),
  cert: fs.readFileSync('cert.pem', 'utf8'),//fullchain
  key: fs.readFileSync('privkey.pem', 'utf8')
})},86400000)
Run Code Online (Sandbox Code Playgroud)