簡體   English   中英

如何通過upstart和monit支持多個nodejs服務?

[英]How to support several nodejs services by upstart and monit?

我創建了一個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

我的monit腳本如下:

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實例,如果失敗則nginx或兩個nodejs實例都關閉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM