gpt4 book ai didi

node.js - 如何通过 upstart 和 monit 支持多个 nodejs 服务?

转载 作者:搜寻专家 更新时间:2023-11-01 00:08:48 24 4
gpt4 key购买 nike

我创建了一个 upstart 脚本来守护 node.js 应用程序:

description "app_name"

start on startup
stop on shutdown

script
export HOME="/home/ubuntu/nodeapp"

exec sudo -u nodejs /usr/local/bin/node $HOME/app/server.js production 2>>/var/log/app_name.error.log >>/var/log/app_name.log
end script

我的监控脚本如下:

check host app_name with address 127.0.0.1
start "/sbin/start app_name"
stop "/sbin/stop app_name"
if failed port 80 protocol HTTP
request /ok
with timeout 5 seconds
then restart

它工作正常,但现在我想添加 nginx 作为上游的负载均衡器,如下所示:

upstream cluster1 {
least_conn;
server 127.0.0.1:8000;
server 127.0.0.1:8001;
}

server {
listen 0.0.0.0:80;

location / {
proxy_pass http://cluster1;
}
}

那么我应该如何更改 upstart 和 monit 脚本来支持它呢?

最佳答案

您应该创建三个 monit 检查,一个用于 nginx,一个用于每个 nodejs 实例:

check host nginx with address 127.0.0.1
start "/sbin/start nginx"
stop "/sbin/stop nginx"
if failed port 80 protocol HTTP
request /ok
with timeout 5 seconds
then restart

check host app_name_on_8000 with address 127.0.0.1
start "/sbin/start app_name_on_8000"
stop "/sbin/stop app_name_on_8000"
if failed port 8000 protocol HTTP
request /ok
with timeout 5 seconds
then restart

check host app_name_on_8001 with address 127.0.0.1
start "/sbin/start app_name_on_8001"
stop "/sbin/stop app_name_on_8001"
if failed port 8000 protocol HTTP
request /ok
with timeout 5 seconds
then restart

这将在它们失败时重新启动 nodejs 实例,如果失败或两个 nodejs 实例都关闭,则重新启动 nginx

关于node.js - 如何通过 upstart 和 monit 支持多个 nodejs 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16496735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com