在节点应用程序中为ALB启用运行状况检查的代码

问题描述 投票:1回答:2

我正在检查以使用Node在AWS中启用Application Load Balancer。在我的EC2实例中,Im使用nginx代理发送到3000端口,而我所配置的只是“ / healthcheck”路径,用于在nginx中验证并结点健康控制。在nginx中很容易

location /healthcheck {
return 200;

}

但是对于节点,我找不到一个清晰的示例来为3002端口启用相同的功能。目前,我有超时错误。

谢谢

PD:我检查了历史记录,最后一个答案是3岁,创建了ping.html文件How to configure AWS Elastic Load Balancer's heath check for Node.js

node.js amazon-web-services amazon-elb health-monitoring
2个回答
0
投票

您基本上需要公开一个healtcheck端点。这将取决于您使用什么来创建Web服务器。选项是expresshttphapijs等。使用express的最简单路线可能是

const express = require('express')
const app = express()
const port = 3002

app.get('/healthcheck', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

在您的ALB中,您可以简单地使用http://someIp:3002/healthcheck。如果返回200,则表示您的应用已启动。您需要确保端口也已打开以在ec2上进行通信。


0
投票

我已经做到了。以我为例,它用于AWS的基于DNS延迟的路由服务,但工作原理相同。

我在我的nodejs程序中设置了一个/health端点。用express做到这一点很简单。然后,将此节放在nginx.conf中。

         location /health {

              proxy_pass http://relay/health;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              proxy_set_header Host $host;
              proxy_set_header        X-Real-IP       $remote_addr;
              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
         }

[所有这些东西都可以说服nginx将原始主机地址传递给nodejs代理。我的服务器称为“中继”,您的服务器则有所不同。

我的/health端点设置为在一切正常时返回200,在特定服务器超载或处于脱机状态时返回503(“服务不可用” a / k / a“稍后重试”)。完全死机的服务器根本没有响应,AWS DNS服务正确解释了该响应,ALB服务也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.