1

我正在负载测试在 AWS 的 ec2 上运行的 NestJS 应用程序。如果我将负载测试工具配置为直接访问 NestJS 应用程序,我可以同时处理数千个请求而不会出现问题。

但是,如果我通过 Application Load Balancer (ALB) 执行相同的负载测试,我开始收到频繁的 502,即使并发请求低至 50 个。

我正在使用 Express 框架,看起来很多人在负载均衡器后面使用 Express 时遇到了类似的问题。通常的建议是将 keepAliveTimeout 和 headersTimeout 设置为大于负载均衡器空闲超时的值,我尝试了几种不同的方法,但都没有解决问题:

const server = await app.listen(config.server_port);
server.keepAliveTimeout = 120 * 1000;
server.headersTimeout = 125 * 1000;

和..

const adapterHost = app.get(HttpAdapterHost);
const httpAdapter = adapterHost.httpAdapter;
const express = httpAdapter.getHttpServer();
express.keepAliveTimeout = 120 * 1000;
express.headersTimeout = 125 * 1000;

似乎 Node 正在发出此事件:https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_event_aborted导致来自 Express ErrorHandler 的此堆栈跟踪:

npm: BadRequestError: request aborted
npm: at IncomingMessage.onAborted (/var/lib/hdai-apps/compute-api/node_modules/raw-body/index.js:231:10)
npm: at IncomingMessage.emit (events.js:315:20)
npm: at IncomingMessage.EventEmitter.emit (domain.js:467:12)
npm: at abortIncoming (_http_server.js:561:9)
npm: at socketOnClose (_http_server.js:554:3)
npm: at TLSSocket.emit (events.js:327:22)
npm: at TLSSocket.EventEmitter.emit (domain.js:467:12)
npm: at net.js:673:12
npm: at Socket.done (_tls_wrap.js:563:7)
npm: at Object.onceWrapper (events.js:422:26)
npm: at Socket.emit (events.js:315:20)
npm: at Socket.EventEmitter.emit (domain.js:467:12)
npm: at TCP.<anonymous> (net.js:673:12)

我创建了一个 Express 错误处理程序,可以捕获更多信息:

{"message":"request aborted","code":"ECONNABORTED","expected":35165,"length":35165,"received":8948,"type":"request.aborted"}

所以 Node 基本上告诉我客户端已断开连接,但 ALB 日志告诉我目标已断开连接。

不知道下一步该去哪里。非常感谢成功将 NestJS/Express 配置为在负载均衡器后面以高负载运行的人的任何建议。谢谢!

4

0 回答 0