Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created January 26, 2014 11:18
Show Gist options
  • Save roman01la/8631278 to your computer and use it in GitHub Desktop.
Save roman01la/8631278 to your computer and use it in GitHub Desktop.
Node.js + Nginx + Varnish + Upstart + Monit
check host localhost with address 127.0.0.1
start "/sbin/start myapp"
stop "/sbin/stop myapp"
if failed port 3000 protocol HTTP
request /
with timeout 5 seconds
then restart
upstream myapp_upstream {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 8080;
server_name myapp.com;
return 301 $scheme://www.myapp.com$request_uri;
}
server {
listen 8080;
server_name www.myapp.com;
error_page 400 404 500 502 503 504 /50x.html;
location /50x.html {
internal;
root /usr/share/nginx/www;
}
location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico|home/|html|xml) {
root /home/ubuntu/myapp/public;
access_log off;
expires max;
}
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://myapp_upstream;
proxy_intercept_errors on;
}
}
#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/myapp git checkout -f
echo "Installing dependencies..."
cd /home/ubuntu/myapp
npm install
echo "Restarting node.js..."
sudo restart myapp
#/etc/init/myapp.conf
description "MyApp Node.js"
author "[email protected]"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 5 60
script
cd /home/ubuntu/myapp
exec sudo -u ubuntu NODE_ENV=production /usr/bin/node /home/ubuntu/myapp/server.js >> /home/ubuntu/myapp/log/server.log 2>&1
end script
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
# force lookup for static assets
if (req.url ~ "\.(png|gif|jpg|swf|css|js|html|ico)$") {
return(lookup);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
sub vcl_fetch {
# strip the cookie before static asset is inserted into cache.
if (req.url ~ "\.(png|gif|jpg|swf|css|js|html|ico)$") {
unset beresp.http.set-cookie;
}
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120 s;
return (hit_for_pass);
}
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment