NodeJS https server returning ERR_SSL_PROTOCOL_ERR

2019-05-08 05:32发布

I'm trying to get SSL https working on my nodejs server but the browser returns a ERR_SSL_PROTOCOL_ERROR

code:

var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');

var options = {
    key: fs.readFileSync('key.pem'),
    cert: fs.readFileSync('cert.pem')
}

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

1条回答
小情绪 Triste *
2楼-- · 2019-05-08 05:44

This will happen if your key isn't generated correctly.

A lot of places will tell you to do this:

openssl genrsa -out key.pem

That will not work if you're on a Mac, and instead you need to do this to make the key length 2048:

openssl genrsa -out key.pem 2048

In summary, do these steps to make a correct key on Mac:

openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out client.csr
openssl x509 -req -in client.csr -signkey key.pem -out cert.pem
查看更多
登录 后发表回答