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 : [cc lang=”bash”] 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; } } [/cc] [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. [cc lang=”bash”] 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; } … … } [/cc] [sysvinit: Init Script] For the benefit of Monit, we will install a sysvinit script: [cc lang=”bash”] $ cd livefortunes $ cp script/livefortunes /etc/init.d/ $ chmod +x /etc/init.d/livefortunes [/cc] Then start and stop our livefortunes node server is as simple as: [cc lang=”bash”] $ /etc/init.d/livefortunes start $ /etc/init.d/livefortunes stop [/cc] [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): [cc lang=”bash”] $ aptitude install monit [/cc] Create a livefortunes monit config file in /etc/monit.d/livefortunes, restart the server if :8000/ doesn’t response in 10 seconds. [cc lang=”bash”] #!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 [/cc] Include additional configuration files in /etc/monit/monitrc: [cc lang=”bash”] include /etc/monit.d/* [/cc] Setup Monit to start automatically and check intervals to 60 seconds in /etc/default/monit : [cc lang=”bash”] startup=1 CHECK_INTERVALS=60 [/cc] [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.

Buy me a coffee
  • Post author: Samson Wu
  • Post link: 3140.html
  • Copyright Notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
0%