mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-05 09:29:25 +08:00
fix: 修复 HTTPS 代理 502、转发头与非 root 端口
- https-server.js 改在 res close 且响应未写完时销毁代理请求,避免 Node 24 请求体读完触发 close 导致 502 - 转发请求补齐 X-Forwarded-Proto=https 与 X-Forwarded-For,供 NextAuth 与来源 IP 统计使用 - 内置 HTTPS 默认监听非特权端口 8443,compose 与文档改为宿主 443 映射到容器 8443 - 补充 HTTPS 代理 GET/POST/默认端口集成测试 Refs: https://github.com/margrop/wrong-notebook/issues/159
This commit is contained in:
@@ -32,8 +32,10 @@ const options = {
|
||||
};
|
||||
|
||||
const port = parseInt(process.env.PORT || '3000', 10);
|
||||
// 非 root 运行时无法绑定 443 特权端口,默认使用 8443,由 compose 映射到宿主 443
|
||||
const httpsPort = parseInt(process.env.HTTPS_PORT || '8443', 10);
|
||||
|
||||
console.log(`[HTTPS] 启动 HTTPS 代理,端口 443 -> HTTP ${port}`);
|
||||
console.log(`[HTTPS] 启动 HTTPS 代理,端口 ${httpsPort} -> HTTP ${port}`);
|
||||
console.log(`[HTTPS] 代理超时设置: ${PROXY_TIMEOUT / 1000} 秒`);
|
||||
|
||||
const httpsServer = https.createServer(options, (req, res) => {
|
||||
@@ -46,6 +48,11 @@ const httpsServer = https.createServer(options, (req, res) => {
|
||||
...req.headers,
|
||||
// 确保 Host 头正确
|
||||
host: req.headers.host || `127.0.0.1:${port}`,
|
||||
// 下游按 HTTPS 处理 Secure Cookie、回调地址与来源 IP
|
||||
'x-forwarded-proto': 'https',
|
||||
'x-forwarded-for': req.headers['x-forwarded-for']
|
||||
? `${req.headers['x-forwarded-for']}, ${req.socket.remoteAddress}`
|
||||
: req.socket.remoteAddress,
|
||||
},
|
||||
// 设置代理请求超时
|
||||
timeout: PROXY_TIMEOUT,
|
||||
@@ -80,9 +87,10 @@ const httpsServer = https.createServer(options, (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 客户端断开连接时,终止代理请求
|
||||
req.on('close', () => {
|
||||
proxyReq.destroy();
|
||||
// 客户端断开连接时终止代理请求;Node 16+ 在请求体读完后也会触发 req close,
|
||||
// 因此监听 res close 并只在响应尚未写完时销毁,避免正常请求被误判为 502
|
||||
res.on('close', () => {
|
||||
if (!res.writableEnded) proxyReq.destroy();
|
||||
});
|
||||
|
||||
// 将请求体转发到代理
|
||||
@@ -94,8 +102,6 @@ httpsServer.timeout = PROXY_TIMEOUT;
|
||||
httpsServer.keepAliveTimeout = 65000; // 65 秒 Keep-Alive
|
||||
httpsServer.headersTimeout = 66000; // 略大于 keepAliveTimeout
|
||||
|
||||
const httpsPort = parseInt(process.env.HTTPS_PORT || '443', 10);
|
||||
|
||||
httpsServer.listen(httpsPort, '0.0.0.0', () => {
|
||||
console.log(`[HTTPS] HTTPS 服务器已启动,监听端口 ${httpsPort}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user