-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.vcl
More file actions
158 lines (133 loc) · 3.22 KB
/
default.vcl
File metadata and controls
158 lines (133 loc) · 3.22 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#
# NetApp StorageGRID
# v1.1
#
vcl 4.1;
import goto;
import kvstore;
import std;
import urlplus;
import utils;
# define health check for S3 endpoint
probe healthcheck {
.request =
"OPTIONS / HTTP/1.1"
"Connection: close"
"User-Agent: Varnish Health Probe";
.timeout = 1s;
.interval = 10s;
.window = 5;
.threshold = 3;
}
# Varnish requires a traditional backend so we need to define a dummy backend never to be used
backend none {
.host = "0";
.port = "0";
}
# custom VCL for handling S3 requests
sub vcl_init {
# define S3 endpoint using Varnish GOTO director
new endpoint = goto.dns_director(s="https://s3.muccbc.hq.netapp.com:8082",probe=healthcheck);
# initialize hit counter as normal cache hit counter does not work due to backend lookups for all requests
new hits = kvstore.init();
}
sub vcl_recv
{
# send all traffic to the director
set req.backend_hint = endpoint.backend();
# Only cache GET requests
if (req.method == "GET") {
# Save the range
if (req.http.Range) {
set req.http.x-range = req.http.Range;
unset req.http.Range;
}
return (hash);
} else {
return (pass);
}
}
sub vcl_hash
{
# Range fragment caching
if (req.http.x-range) {
hash_data(req.http.x-range);
}
# Strip query params
hash_data(urlplus.url_get());
# Hash on host
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
sub vcl_hit
{
return (pass);
}
sub vcl_backend_fetch
{
# Restore the range
if (bereq.http.x-range) {
set bereq.http.Range = bereq.http.x-range;
unset bereq.http.x-range;
}
}
sub vcl_backend_response
{
# Errors
if (beresp.status >= 400 && beresp.status != 404) {
return (error(beresp.status, beresp.reason));
}
# We do not have an actual object
if (!beresp.http.etag) {
set beresp.uncacheable = true;
return (deliver);
}
# Calculate a hit counter
if (beresp.was_304) {
hits.counter(urlplus.url_get(), 1);
} else {
hits.delete(urlplus.url_get());
}
# Workaround to allow 206 responses to be ETagged in future requests
unset beresp.http.X-206;
if (beresp.status == 206) {
set beresp.http.X-206 = "true";
set beresp.status = 200;
if (beresp.http.Content-Range) {
set beresp.http.x-content-range = beresp.http.Content-Range;
unset beresp.http.Content-Range;
}
}
# TTL
set beresp.ttl = 0.000001s;
set beresp.grace = 0s;
set beresp.keep = 10y;
utils.fast_304();
return (deliver);
}
sub vcl_backend_error
{
set beresp.ttl = 0s;
set beresp.grace = 0s;
set beresp.keep = 0s;
set beresp.uncacheable = true;
return (deliver);
}
sub vcl_deliver
{
# Restore 206 response
if (resp.http.X-206 == "true") {
set resp.status = 206;
unset resp.http.X-206;
}
if (resp.http.x-content-range) {
set resp.http.Content-Range = resp.http.x-content-range;
unset resp.http.x-content-range;
}
# Hits
set resp.http.x-hits = hits.counter(urlplus.url_get(), 0);
}