Node.js返回HTTPS的空响应
我有以下非常基本的Node.js服务器:
"use strict"; const http = require("http"); const https = require("https"); const fs = require("fs"); http.createServer((req, res) => { console.log("regular works"); res.end("Regular response"); }).listen(3000); https.createServer({ key: fs.readFileSync("/etc/letsencrypt/live/domain.com/privkey.pem"), cert: fs.readFileSync("/etc/letsencrypt/live/domain.com/cert.pem") }, (req, res) => { console.log("secure works"); res.end("Secure response"); }).listen(3001);
我把它作为sudo node filename.js
运行,只是因为/etc/letsencrypt/live
中的文件是仅限于root的。 我会稍后做这个,这只是为了testing。
运行时,我可以打3000端口就好了。 服务器控制台打印regular works
,浏览器显示“ Regular response
。 但是,端口3001返回一个空的响应,并没有消息打印到服务器。
LetsEncrypt文件是使用./letsencrypt-auto certonly --standalone -d domain.com --email email@gmail.com --agree-tos
并显示为有效。
我错过了什么预期的结果?
这里有两个问题:
-
假设你没有模糊真正的主机名/ IP,你应该使用127.0.0.1或类似的(如果你在同一台机器上)而不是255.255.255.255。
-
HTTP是cURL的默认值,因此您当前正在向HTTPS服务器发送纯文本HTTP请求,但HTTPS服务器无法正常工作(HTTPS服务器将文字HTTP请求视为无效的TLS握手,从而导致连接突然终止) 。 为了解决这个问题,请明确包含
https://
(例如curl -I --verbose https://127.0.0.1:3001
)。