如何配置AWS弹性负载均衡器's对Node.js的健康检查

How to configure AWS Elastic Load Balancer's heath check for Node.js

本文关键字:Node js 检查 健康 均衡器 配置 何配置 AWS 负载      更新时间:2023-09-26

我是Node.js的新手,我的问题对别人来说可能很愚蠢,请原谅我。

过去,我在EC2实例上安装了Apache。我在这个目录中创建了ping.html/var/www/html/ping.html。

并且运行状况检查将是:Ping端口:80Ping路径:/Ping.html

我不知道如何为Node.js做同样的事情。我应该在哪里创建这个ping.html文件,因为Node.js没有这样的目录/var/www/html/?或者我应该怎么做才能通过Node.js的ELB健康检查?

我读了很多说明,但似乎对我来说"太多了"。

这取决于你想检查什么以及你在node.js中使用了什么。如果你只是想检查以确保node.js服务器正在运行,你可以简单地将其添加到HTTP服务器:

var server = http.createServer(function(request, response) {
    if (request.url === '/ping.html' && request.method ==='GET') {
        //AWS ELB pings this URL to make sure the instance is running
        //smoothly
        response.writeHead(200, {
            'Content-Type': 'text/plain',
            'Content-Length': 2
        });
        response.write('OK');
        response.end();
    }
    //Do the rest of your web server here
});
server.listen(8080 /* Whatever port */);

如果您在节点中使用的东西为express.js等内容提供服务器,那么您可以让ELB检查express.js服务的页面的内容。