running node.js as a service behind nginx/varnish
TRANSCRIPT
Running node.js
as a service behind ngnix/varnish
why?
$ ssh [email protected] me@srv:~$ node /opt/myapp/index.js
$ ssh [email protected] me@srv:~$ node /opt/myapp/index.js X
$ ssh [email protected] me@srv:~$ screen
Screen version 4.00.03jw4 (FAU) 2-May-06
Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder Copyright (c) 1987 Oliver Laumann
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to [email protected]
To use byobu (formerly screen-profiles), exit screen and run 'byobu'.
[Press Space or Return to end.]
me@srv:~$ node /opt/myapp/index.js
me@srv:~$ node /opt/myapp/index.js [detached from 2254.pts-0.srv] me@srv:~$
me@srv:~$ node /opt/myapp/index.js [detached from 2254.pts-0.srv] me@srv:~$ X
me@srv:~$ while true ; do > node /opt/myapp/index.js > done
me@srv:~$ while true ; do > node /opt/myapp/index.js > done X
•forever https://github.com/foreverjs/forever
•monit https://mmonit.com/monit/
•pm2 http://pm2.keymetrics.io
•systemd https://freedesktop.org/wiki/Software/systemd/
•upstart http://upstart.ubuntu.com
•etc …
upstart
# /opt/myapp/index.js
require("http").createServer(function (req, res) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end("<h1>Hello, Fullstack JS Berlin!</h1>");
}).listen(1337, "0.0.0.0");
$ sudo vi /etc/init/myapp.conf
# /etc/init/myapp.conf
description "myapp"
author "thiagoalessio"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
exec sudo -u myapp /usr/bin/node /opt/myapp/index.js >> /var/log/myapp.log 2>&1
end script
post-start script
echo "myapp was started" >> /opt/myapp/logs/myapp.log
end script
$ sudo start myapp myapp start/running, process 1351
$ sudo stop myapp myapp stop/waiting
•automatically starts on machine boot
•easily update code after deployments
super fast!
$ sudo vi /etc/varnish/default.vcl
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "127.0.0.1";
.port = "1337";
}
#
# … rest of the file …
$ sudo stop myapp myapp stop/waiting
concurrency
$ sudo vi /etc/nginx/sites-enabled/default
# /etc/nginx/sites-enabled/default
upstream varnish { server 127.0.0.1:6081; }
upstream nodejs { server 127.0.0.1:1337; }
server {
listen 80;
server_name _;
root /opt/myapp;
location ~* \.(jpg|css|png)$ { access_log off; }
location /non-cacheable-content { proxy_pass http://nodejs; }
location / { proxy_pass http://varnish; }
}