certbot certonly
的 SSL 证书(此处替换为 example.com
)console.log
它们时它们看起来是正确的-----BEGIN CERTIFICATE-----
和 -----END CERTIFICATE-----
行,这似乎不是案例在这里。当我尝试使用 https 启动服务器时,
https
模块给我错误 error:0480006C:PEM routines::no start line
。
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const { config } = require('dotenv');
config();
const app = express();
const httpPort = process.env.HTTP_PORT;
const httpsPort = process.env.HTTPS_PORT;
try {
const privateKeyPath = process.env.SSL_KEY;
const publicKeyPath = process.env.SSL_CERT;
const privateKey = fs.readFileSync(privateKeyPath, 'utf8');
const certificate = fs.readFileSync(publicKeyPath, 'utf8');
const credentials = { key: privateKey, cert: certificate };
const httpsServer = https.createServer(credentials, app);
httpsServer.listen(httpsPort, () => {
console.log(`HTTPS Server listening on port ${httpsPort}`);
});
} catch (ex) {
console.error('Certificates not found. Not using HTTPS');
console.error(ex);
}
const httpServer = http.createServer(app);
httpServer.listen(httpPort, () => {
console.log(`HTTP Server listening on port ${httpPort}`);
});
HTTP_PORT=80
HTTPS_PORT=443
SSL_KEY=/etc/letsencrypt/live/example.com/fullchain.pem
SSL_CERT=/etc/letsencrypt/live/example.com/privkey.pem
-----BEGIN CERTIFICATE-----
...base64 encoded text here
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
...base64 encoded text here
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
...base64 encoded text here
-----END CERTIFICATE-----
-----BEGIN PRIVATE KEY-----
...base64 encoded text here
-----END PRIVATE KEY-----
Certificates not found. Not using HTTPS
Error: error:0480006C:PEM routines::no start line
at node:internal/tls/secure-context:69:13
at Array.forEach (<anonymous>)
at setCerts (node:internal/tls/secure-context:67:3)
at configSecureContext (node:internal/tls/secure-context:156:5)
at Object.createSecureContext (node:_tls_common:117:3)
at Server.setSecureContext (node:_tls_wrap:1348:27)
at Server (node:_tls_wrap:1207:8)
at new Server (node:https:74:3)
at Object.createServer (node:https:112:10)
at Object.<anonymous> (/root/reponame/app.js:78:29) {
library: 'PEM routines',
reason: 'no start line',
code: 'ERR_OSSL_PEM_NO_START_LINE'
}
正如@DivineSoul所述,问题是我的私钥路径和公钥路径切换错误。
过去两天我一直在解决这个问题,我也遇到了同样的问题。你是怎么解决的?谢谢