Deploying Live Fortune (Node.js) with Nginx and Monit

Here we will use Nginx to serve static resources and reverse proxy dynamic requests to the Node server. We will also use Monit to monitor the Node server and restart it automatically in case it crash. This is also the deployment I did at fortune.crash4.us.

[Nginx: Static Resources]
livefortunes use express.static to server static resources, but for better performance we will use nginx to do the job that it’s designed and built to do.

Config nginx to server all the static resources which located under /public :

server {
    listen       80;
    server_name  fortune.crash4.us;
    root         /path/to/livefortunes/public;
    index        index.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
         root   html;
    }
}


[Nginx: Reverse Proxy]
Our node server started separately and listened at another port (in this case, 8000), config nginx to reverse proxy all the dynamic requests to our backend node servers.

upstream node_server {
    server 127.0.0.1:8000;
}
server {
    ... ...
    try_files  $uri/index.html $uri.html $uri @node_server;

    location @node_server {
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header  Host             $http_host;
        proxy_redirect    off;
        proxy_pass        http://node_server;
    }
    ... ...
}

[sysvinit: Init Script]
For the benefit of Monit, we will install a sysvinit script:

$ cd livefortunes
$ cp script/livefortunes /etc/init.d/
$ chmod +x /etc/init.d/livefortunes

Then start and stop our livefortunes node server is as simple as:

$ /etc/init.d/livefortunes start
$ /etc/init.d/livefortunes stop

[Monit: Auto Restart]
Node server seems pretty enjoy crashing, from my experience, it crashes at least 2 times a day. Considering the site heavily relies on our node server, this is totally unacceptable. Thanks to Monit, we can restart Node server in a manageable and flexible way.

Install Monit (debian, ubuntu):

$ aptitude install monit

Create a livefortunes monit config file in /etc/monit.d/livefortunes, restart the server if :8000/ doesn’t response in 10 seconds.

#!monit
set logfile /var/log/monit.log

check host livefortunes with address 127.0.0.1
    start program = "/etc/init.d/livefortunes start"
    stop program  = "/etc/init.d/livefortunes stop"
    if failed port 8000 protocol HTTP
        request /
        with timeout 10 seconds
        then restart

Include additional configuration files in /etc/monit/monitrc:

include /etc/monit.d/*

Setup Monit to start automatically and check intervals to 60 seconds in /etc/default/monit :

startup=1
CHECK_INTERVALS=60

[Conclusion]
Node server seems take more memory than my expectation, in my case, it takes more memory than nginx and mysql…

LiveFortunes is still under development, for the latest code, please check out its github repository.

Related posts:

Got something to say? Go for it!

使用新浪微博登陆