我在 EC2 上有一个带有 HTTP 集成后端服务器的 HTTP API 网关。该 API 白天有很多查询,查看日志后我意识到该 API 有时会返回带有正文的 503 HTTP 代码:
{ "message": "Service Unavailable" }
当我发现这一点时,我尝试了 API 并在 Postman 上多次运行 HTTP 请求,当我尝试二十次时,我至少得到一个 503。
然后我认为 HTTP 集成服务器很忙,但服务器未加载,我尝试直接访问 HTTP 集成服务器,但总是收到 200 个响应。
超时参数设置为30000ms,端点平均响应时间为200ms,所以超时不是问题。此外,HTTP 503 不是在请求 30 秒后出现,而是立即出现。
有人可以帮助我吗?
谢谢
我通过编辑内部集成服务器的保持活动连接参数解决了这个问题。 AWS API Gateway 需要标准配置上的保持活动参数,因此我开始调整 NGINX 服务器参数,直到解决问题。
在集成到 AWS API-Gateway 中的带有 Node 的自制微服务上也遇到同样的问题。对 Cloudwatch-Logs 进行一些重新配置后,我得到了有关问题所在的进一步指示:
INTEGRATION_NETWORK_FAILURE
在 API-Gateway - Logging 中以“日志格式”添加更多输出 使用此内容或类似内容作为“日志格式”:
{"httpMethod":"$context.httpMethod","integrationErrorMessage":"$context.integrationErrorMessage","protocol":"$context.protocol","requestId":"$context.requestId","requestTime":"$context.requestTime","resourcePath":"$context.resourcePath","responseLength":"$context.responseLength","routeKey":"$context.routeKey","sourceIp":"$context.identity.sourceIp","status":"$context.status","errMsg":"$context.error.message","errType":"$context.error.responseType","intError":"$context.integration.error","intIntStatus":"$context.integration.integrationStatus","intLat":"$context.integration.latency","intReqID":"$context.integration.requestId","intStatus":"$context.integration.status"}
使用 API-Gateway Endpoint 并再次查阅日志失败后 - 应该如下所示:
在侦听时添加标头超时并在快速服务器套接字配置上保持活动状态。
const app = require('express')();
// if not already set and required to advertise the keep-alive through HTTP-Response you might want to use this
/*
app.use((req: Request, res: Response, next: NextFunction) => {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Keep-Alive', 'timeout=30');
next();
});
*/
/* ..you r main logic.. */
const server = app.listen(8080, 'localhost', () => {
console.warn(`⚡️[server]: Server is running at http://localhost:8080`);
});
server.keepAliveTimeout = 30 * 1000; // <- important lines
server.headersTimeout = 35 * 1000; // <- important lines
某些 AWS 组件似乎要求连接保持活动状态 - 即使服务器以其他方式响应 (
connection: close
)。在 API Gateway(可能还有 AWS ELBs)中重用时,回收将失败,因为另一端很可能已经关闭,因此假定“网络故障”。
此错误似乎是间歇性的 - 因为至少 API 网关似乎在一段时间后关闭未使用的连接,以便下次提供干净的执行。我只能假设他们这样做是为了高性能,而不是为了低性能。
我在 AWS CDK Stack 上遇到了类似的问题,该堆栈为一些应使用 API 网关访问的 EC2 服务创建了 ECS 集群。但由于某种原因,API 网关不断响应“服务不可用”,即使我能够通过负载均衡器访问它(在暂时公开负载均衡器之后)。
我在为 ECS 集群创建 VPC 时将
restrictDefaultSecurityGroup
设置为 false
成功解决了该问题:
const vpc = new ec2.Vpc(
this,
'ClusterVpc',
{
maxAzs: 2,
// Add this:
restrictDefaultSecurityGroup: false,
},
);
这将禁止删除某些安全组规则,而这些规则是整个功能正常运行所需的。