node https 模块给出“错误:0480006C:PEM 例程::无起始行”错误,尽管证书是正确的

问题描述 投票:0回答:2

概要

  • 我已经为某个域生成了一组带有
    certbot certonly
    的 SSL 证书(此处替换为
    example.com
  • 我设置了一个快速服务器来使用这些证书,读取这些文件似乎没有任何问题,因为当我
    console.log
    它们时它们看起来是正确的
  • 证书文件,至少在我看来,看起来格式正确(具有正确的标题和起始行)
  • 我知道存在类似的问题,但在我能找到的所有问题中,问题是证书和私钥文件中缺少
    -----BEGIN CERTIFICATE-----
    -----END CERTIFICATE-----
    行,这似乎不是案例在这里。

问题

当我尝试使用 https 启动服务器时,

https
模块给我错误
error:0480006C:PEM routines::no start line

代码

app.js

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}`);
});

.env

HTTP_PORT=80
HTTPS_PORT=443
SSL_KEY=/etc/letsencrypt/live/example.com/fullchain.pem
SSL_CERT=/etc/letsencrypt/live/example.com/privkey.pem

全链.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-----

privkey.pem

-----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'
}
javascript node.js ssl https certbot
2个回答
4
投票

正如@DivineSoul所述,问题是我的私钥路径和公钥路径切换错误。


0
投票

过去两天我一直在解决这个问题,我也遇到了同样的问题。你是怎么解决的?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.