-
Notifications
You must be signed in to change notification settings - Fork 46
/
default.vcl
executable file
·177 lines (157 loc) · 4.45 KB
/
default.vcl
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
backend default {
.host = "127.0.0.1";
.port = "80";
.first_byte_timeout = 300s;
}
sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
if (req.http.https) {
set req.hash += req.http.https;
}
return (hash);
}
acl cache_acl {
"127.0.0.1";
# insert additional ip's here
}
/*
Like the default function, only that cookies don't prevent caching
*/
sub vcl_recv {
# if (req.http.Host != "varnish.demo.aoemedia.de") {
# return (pipe);
# }
# see http://www.varnish-cache.org/trac/wiki/VCLExampleNormalizeAcceptEncoding
### parse accept encoding rulesets to normalize
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|mp4|flv)$") {
# don't try to compress already compressed files
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.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);
}
# Some known-static file types
if (req.url ~ "^[^?]*\.(css|js|htc|xml|txt|swf|flv|pdf|gif|jpe?g|png|ico)$") {
# Pretent no cookie was passed
unset req.http.Cookie;
}
# Force lookup if the request is a no-cache request from the client.
if (req.http.Cache-Control ~ "no-cache") {
if (client.ip ~ cache_acl) {
purge_url(req.url);
error 200 "Purged.";
} else {
error 405 "Not allowed.";
}
}
# PURGE requests
if (req.request == "PURGE") {
if (client.ip ~ cache_acl) {
purge_url(req.url);
error 200 "Purged.";
} else {
error 405 "Not allowed.";
}
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
/*
Remove cookies from backend response so this page can be cached
*/
sub vcl_fetch {
# set minimum timeouts to auto-discard stored objects
set beresp.grace = 600s;
if (beresp.http.aoestatic == "cache") {
remove beresp.http.Set-Cookie;
remove beresp.http.X-Cache;
remove beresp.http.Server;
remove beresp.http.Age;
remove beresp.http.Pragma;
set beresp.http.Cache-Control = "public";
set beresp.grace = 2m;
set beresp.http.X_AOESTATIC_FETCH = "Removed cookie in vcl_fetch";
set beresp.cacheable = true;
} else {
set beresp.http.X_AOESTATIC_FETCH = "Nothing removed";
}
# Don't cache negative lookups
if (beresp.status >= 400) {
set beresp.ttl = 0s;
set beresp.http.X_AOESTATIC_FETCH_PASSREASON = "Status greater than 400";
return(pass);
}
if (!beresp.cacheable) {
set beresp.http.X_AOESTATIC_FETCH_PASSREASON = "Not cacheable";
return(pass);
}
if (req.http.Authorization) {
set beresp.http.X_AOESTATIC_FETCH_PASSREASON = "HTTP AUTH";
return(pass);
}
# Some known-static file types
if (req.url ~ "^[^?]*\.(css|js|htc|xml|txt|swf|flv|pdf|gif|jpe?g|png|ico)\$") {
# Force caching
remove beresp.http.Pragma;
remove beresp.http.Set-Cookie;
set beresp.http.Cache-Control = "public";
}
if (beresp.http.Set-Cookie) {
set beresp.http.X_AOESTATIC_FETCH_PASSREASON = "Cookie";
return(pass);
}
if (!beresp.http.Cache-Control ~ "public") {
set beresp.http.X_AOESTATIC_FETCH_PASSREASON = "Cache-Control is not public";
return(pass);
}
if (beresp.http.Pragma ~ "(no-cache|private)") {
set beresp.http.X_AOESTATIC_FETCH_PASSREASON = "Pragma is no-cache or private";
return(pass);
}
}
/*
Adding debugging information
*/
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT ("obj.hits")";
set resp.http.Server = "Varnish (HIT: "obj.hits")";
} else {
set resp.http.X-Cache = "MISS";
set resp.http.Server = "Varnish (MISS)";
}
}