【发布时间】:2015-11-18 05:15:10
【问题描述】:
我无法让服务器在 HTTPS 工作 node.js 之后运行。我的目标是让 Hapi 服务器使用自签名证书运行。
这就是我生成自签名证书的方式:
openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
rm csr.pem
这就是我在 Hapi 中启动服务器的方式:
server.connection({
port: 9920,
tls: {
key: fs.readFileSync(path.resolve(__dirname, './cert/key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, './cert/cert.pem')),
rejectUnauthorized: false
},
routes: {
cors: true
}
});
为了验证这不是 Hapi 唯一的问题,我验证这在 vanilla node.js 中也不起作用:
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync(path.resolve(__dirname, './cert/key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, './cert/cert.pem')),
rejectUnauthorized: false
};
var a = https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
这两个服务器都在 curl 中抛出相同的错误:
$ curl -kiv https://localhost:9920
* Rebuilt URL to: https://localhost:9920/
* Trying ::1...
* connect to ::1 port 9920 failed: Connection refused
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9920 (#0)
* Server aborted the SSL handshake
* Closing connection 0
curl: (35) Server aborted the SSL handshake
版本:
- 节点:v4.1.1
- npm: 3.4.0
发生了什么事?
【问题讨论】:
-
如果服务器中止握手,您需要查看服务器端的错误,而不是客户端。