设置 Node.js HTTPS 以使用 HAPROXY
Setup Node.js HTTPS to work with HAPROXY
我正在尝试让我的 nodejs 应用程序通过 https 与 HAPROXY 进行通信。这个想法是 nodejs 通过 https 将消息发送到 haproxy,haproxy 路由消息转发。
我使用了 request.js 库并且一切正常,但现在我需要在没有任何库的情况下执行此任务。场景如下。如果环境变量是 1,我应该使用 HTTP,在其他情况下 -HTTPS。问题是,当我使用 https 和 haproxy 时,我得到 "Socket hangup error",但使用 request.js 一切正常。这是我的代码。
const protocol = process.env.NODE_ENV === 1 ? require('http') : require('https');
然后我配置HTTPS
this.api = url.parse(app.get('API_HAPROXY'));
this.options = {
port: this.api.port,
hostname: this.api.hostname,
path: '/api/report',
method: 'POST',
headers: {
"Content-Type": "application/json",
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};
因为我不想使用 ca 来验证我使用的 ssh 密钥 NODE_TLS_REJECT_UNAUTHORIZED=0
reportData(json) {
const req = protocol.request(this.options, (res) => {
res.on('error', (err) => {
this.logger.error(`Failed to report ${err.message}`)
})
});
req.write(JSON.stringify(json));
req.end();
req.on('error', (err) => {
this.logger.error(`Failed to report ${err.message}`);
});
}
在这种情况下,我在使用 HTTPS 时遇到套接字挂起错误
这是我的请求配置
request({
uri: `${this.api}/api/report`,
method: 'POST',
json,
}, (err, response) => {
if (err || response.statusCode !== 200) {
this.logger.error(`Failed to report : ${err ? err.message : response.statusCode}`);
} else {
this.logger.info(`Report was sent`);
}
});
已通过将 content-length header 添加到 options.headers 来解决此问题。
this.api = url.parse(app.get('API_HAPROXY')); this.options = {
port: this.api.port,
hostname: this.api.hostname,
path: '/api/report',
method: 'POST',
headers: {
"Content-Type": "application/json",
"Content-Length: <calculated length of the object you want to send in bytes >
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};
我正在尝试让我的 nodejs 应用程序通过 https 与 HAPROXY 进行通信。这个想法是 nodejs 通过 https 将消息发送到 haproxy,haproxy 路由消息转发。
我使用了 request.js 库并且一切正常,但现在我需要在没有任何库的情况下执行此任务。场景如下。如果环境变量是 1,我应该使用 HTTP,在其他情况下 -HTTPS。问题是,当我使用 https 和 haproxy 时,我得到 "Socket hangup error",但使用 request.js 一切正常。这是我的代码。
const protocol = process.env.NODE_ENV === 1 ? require('http') : require('https');
然后我配置HTTPS
this.api = url.parse(app.get('API_HAPROXY'));
this.options = {
port: this.api.port,
hostname: this.api.hostname,
path: '/api/report',
method: 'POST',
headers: {
"Content-Type": "application/json",
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};
因为我不想使用 ca 来验证我使用的 ssh 密钥 NODE_TLS_REJECT_UNAUTHORIZED=0
reportData(json) {
const req = protocol.request(this.options, (res) => {
res.on('error', (err) => {
this.logger.error(`Failed to report ${err.message}`)
})
});
req.write(JSON.stringify(json));
req.end();
req.on('error', (err) => {
this.logger.error(`Failed to report ${err.message}`);
});
}
在这种情况下,我在使用 HTTPS 时遇到套接字挂起错误
这是我的请求配置
request({
uri: `${this.api}/api/report`,
method: 'POST',
json,
}, (err, response) => {
if (err || response.statusCode !== 200) {
this.logger.error(`Failed to report : ${err ? err.message : response.statusCode}`);
} else {
this.logger.info(`Report was sent`);
}
});
已通过将 content-length header 添加到 options.headers 来解决此问题。
this.api = url.parse(app.get('API_HAPROXY')); this.options = {
port: this.api.port,
hostname: this.api.hostname,
path: '/api/report',
method: 'POST',
headers: {
"Content-Type": "application/json",
"Content-Length: <calculated length of the object you want to send in bytes >
},
rejectUnauthorized: false,
requestCert: true,
agent: false
};