public
Description: Run rails applications reliably as runit services
Homepage: http://www.sanityinc.com/
Clone URL: git://github.com/purcell/rails-runit.git
Click here to lend your support to: rails-runit and make a donation at www.pledgie.com !
rails-runit / varnish-run
100755 137 lines (121 sloc) 2.664 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh -e
 
PORT=$(basename $(pwd)|awk -F- '{print $2}')
BACKEND_PORT=$(basename $(pwd)|awk -F- '{print $3}')
USER=$(id -un)
BASE=$(dirname $(readlink $0))
APP_DIR=$($BASE/readlink_canonical $BASE/app)
 
if [ -e $BASE/varnishd ]; then
VARNISHD=$BASE/varnishd
else
VARNISHD=varnishd
fi
 
TEMP_BASE_DIR=/var/tmp/${USER}-varnish-${PORT}
if [ ! -d $TEMP_BASE_DIR ]; then
mkdir -p $TEMP_BASE_DIR;
  chmod og-rwx $TEMP_BASE_DIR;
fi
 
cat <<EOF > varnish.conf
backend default {
set backend.host = "127.0.0.1";
set backend.port = "$BACKEND_PORT";
}
 
# only allow purging of cache from localhost
acl purge {
"localhost";
}
 
sub vcl_recv {
# * Normalize host headers to limit cache usage
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|zip|avi|mov|mp3|ogg)$") {
remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unkown algorithm
remove req.http.Accept-Encoding;
}
}
if (req.request != "GET" && req.request != "HEAD") {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
lookup;
} else {
pipe;
}
}
if (req.http.Expect) {
pipe;
}
# pass to backend if we have HTTP authentication
if (req.http.Authenticate || req.http.Authorization) {
pass;
}
if (req.http.Cache-Control ~ "no-cache") {
pass;
}
lookup;
}
 
sub vcl_pipe {
pipe;
}
sub vcl_pass {
pass;
}
 
sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.host;
hash;
}
 
sub vcl_hit {
# Ensure assets with a timestamp are cached forever
if (obj.http.X-FromFile == "yes" && req.url ~ "\?[0-9]+") {
deliver;
}
if (!obj.cacheable) {
pass;
}
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged";
}
deliver;
}
 
sub vcl_miss {
if (req.http.If-Modified-Since) {
pass;
}
if (req.request == "PURGE") {
error 404 "Not in cache";
}
fetch;
}
 
sub vcl_fetch {
if (!obj.valid) {
error;
}
if (!obj.cacheable) {
pass;
}
if (obj.http.Set-Cookie) {
pass;
}
if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") {
pass;
}
insert;
}
 
sub vcl_deliver {
deliver;
}
 
sub vcl_timeout {
discard;
}
 
sub vcl_discard {
discard;
}
EOF
 
exec $VARNISHD -a 127.0.0.1:$PORT -F -f varnish.conf -s file,$TEMP_BASE_DIR -n $TEMP_BASE_DIR || echo "do you need to create the symlink '$BASE/varnishd?'"