/
default.vcl.j2
114 lines (90 loc) · 2.59 KB
/
default.vcl.j2
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
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub detect_device {
# Define the desktop device
set req.http.X-Device = "desktop";
if (req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" || req.http.User-Agent ~ "iPad") {
# Define smartphones and tablets
set req.http.X-Device = "smart";
}
elseif (req.http.User-Agent ~ "SymbianOS" || req.http.User-Agent ~ "^BlackBerry" ||
req.http.User-Agent ~ "^SonyEricsson" || req.http.User-Agent ~ "^Nokia" ||
req.http.User-Agent ~ "^SAMSUNG" || req.http.User-Agent ~ "^LG"
) {
# Define every other mobile device
set req.http.X-Device = "other";
}
}
sub vcl_recv {
call detect_device;
# About graceful mode: >
# http://siguniang.wordpress.com/2013/10/22/varnish-cache-grace-saint-god/
set req.grace = 300s;
if (req.url == "/lifecheck.txt") {
return (error);
}
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") {
return (pass);
}
if (req.http.Authorization) {
return (pass);
}
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
}
elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
}
else {
# Unknown or deflate algorithm
remove req.http.Accept-Encoding;
}
}
return(lookup);
}
sub vcl_fetch {
return(deliver);
}
sub vcl_hash {
# Cookie毎にキャッシュ
hash_data(req.http.cookie);
# Device毎にキャッシュ
hash_data(req.http.X-Device);
# HTML, JS, CSS毎にキャッシュ
hash_data(req.http.Accept);
# URL毎にキャッシュ
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}
sub vcl_error {
set obj.http.Content-Type = "text/plain; charset=utf-8";
if (req.url == "/lifecheck.txt") {
set obj.status = 200;
synthetic {"lifecheck"};
}
return (deliver);
}
# sub vcl_deliver {
# if (obj.hits > 0) {
# set resp.http.X-Cache = "HIT";
# } else {
# set resp.http.X-Cache = "MISS";
# }
# }