running node.js as a service behind nginx/varnish

53
Running node.js as a service behind ngnix/varnish

Upload: thiago-alessio-pereira

Post on 09-Jan-2017

336 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Running node.js as a service behind nginx/varnish

Running node.js

as a service behind ngnix/varnish

Page 2: Running node.js as a service behind nginx/varnish

why?

Page 3: Running node.js as a service behind nginx/varnish

$ ssh [email protected] me@srv:~$ node /opt/myapp/index.js

Page 4: Running node.js as a service behind nginx/varnish

$ ssh [email protected] me@srv:~$ node /opt/myapp/index.js X

Page 5: Running node.js as a service behind nginx/varnish

$ ssh [email protected] me@srv:~$ screen

Page 6: Running node.js as a service behind nginx/varnish

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.]

Page 7: Running node.js as a service behind nginx/varnish

me@srv:~$ node /opt/myapp/index.js

Page 8: Running node.js as a service behind nginx/varnish

me@srv:~$ node /opt/myapp/index.js [detached from 2254.pts-0.srv] me@srv:~$

Page 9: Running node.js as a service behind nginx/varnish

me@srv:~$ node /opt/myapp/index.js [detached from 2254.pts-0.srv] me@srv:~$ X

Page 10: Running node.js as a service behind nginx/varnish

me@srv:~$ while true ; do > node /opt/myapp/index.js > done

Page 11: Running node.js as a service behind nginx/varnish

me@srv:~$ while true ; do > node /opt/myapp/index.js > done X

Page 12: Running node.js as a service behind nginx/varnish

•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 …

Page 13: Running node.js as a service behind nginx/varnish

upstart

Page 14: Running node.js as a service behind nginx/varnish

# /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");

Page 15: Running node.js as a service behind nginx/varnish

$ sudo vi /etc/init/myapp.conf

Page 16: Running node.js as a service behind nginx/varnish

# /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

Page 17: Running node.js as a service behind nginx/varnish

$ sudo start myapp myapp start/running, process 1351

Page 18: Running node.js as a service behind nginx/varnish
Page 19: Running node.js as a service behind nginx/varnish

$ sudo stop myapp myapp stop/waiting

Page 20: Running node.js as a service behind nginx/varnish
Page 21: Running node.js as a service behind nginx/varnish

•automatically starts on machine boot

•easily update code after deployments

Page 22: Running node.js as a service behind nginx/varnish
Page 23: Running node.js as a service behind nginx/varnish
Page 24: Running node.js as a service behind nginx/varnish
Page 25: Running node.js as a service behind nginx/varnish
Page 26: Running node.js as a service behind nginx/varnish
Page 27: Running node.js as a service behind nginx/varnish
Page 28: Running node.js as a service behind nginx/varnish
Page 29: Running node.js as a service behind nginx/varnish
Page 30: Running node.js as a service behind nginx/varnish
Page 31: Running node.js as a service behind nginx/varnish
Page 32: Running node.js as a service behind nginx/varnish
Page 33: Running node.js as a service behind nginx/varnish
Page 34: Running node.js as a service behind nginx/varnish
Page 35: Running node.js as a service behind nginx/varnish
Page 36: Running node.js as a service behind nginx/varnish

super fast!

Page 37: Running node.js as a service behind nginx/varnish

$ sudo vi /etc/varnish/default.vcl

Page 38: Running node.js as a service behind nginx/varnish

# 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 …

Page 39: Running node.js as a service behind nginx/varnish
Page 40: Running node.js as a service behind nginx/varnish

$ sudo stop myapp myapp stop/waiting

Page 41: Running node.js as a service behind nginx/varnish
Page 42: Running node.js as a service behind nginx/varnish
Page 43: Running node.js as a service behind nginx/varnish
Page 44: Running node.js as a service behind nginx/varnish
Page 45: Running node.js as a service behind nginx/varnish
Page 46: Running node.js as a service behind nginx/varnish
Page 47: Running node.js as a service behind nginx/varnish
Page 48: Running node.js as a service behind nginx/varnish
Page 49: Running node.js as a service behind nginx/varnish

concurrency

Page 50: Running node.js as a service behind nginx/varnish

$ sudo vi /etc/nginx/sites-enabled/default

Page 51: Running node.js as a service behind nginx/varnish

# /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; }

}

Page 52: Running node.js as a service behind nginx/varnish
Page 53: Running node.js as a service behind nginx/varnish

thank you ;D @thiagoalessio

github.com/thiagoalessio