-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.vcl
177 lines (125 loc) · 3.81 KB
/
entrypoint.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
vcl 4.0;
import std;
import var;
include "backends.vcl";
sub vcl_init {
call init_backends;
return (ok);
}
sub vcl_recv {
if (req.restarts > 0) {
set req.hash_always_miss = true;
}
if (req.method == "GET" && req.url == "/heartbeat") {
return(synth(200, "OK"));
}
// If backends are not configured correctly
if (!(var.global_get("backendsFound") == "true")) {
return(synth(503, "No backends configured"));
}
set req.backend_hint = container_rr.backend();
if (req.method == "GET" && req.url == "/liveness") {
if (!std.healthy(req.backend_hint)) {
return(synth(503, "No healthy backends"));
}
return(synth(200, "OK"));
}
// Do not cache paths with health (healthcheck cache fix)
if (req.url ~ "health") {
return (pass);
}
if (req.method == "PURGE") {
# if (client.ip !~ purge) {
# return (synth(405, "Method Not Allowed"));
# }
set req.hash_always_miss = true;
return (purge);
}
# =======================================
return (hash);
}
sub vcl_synth {
set resp.http.Content-Type = "text/html; charset=utf-8";
if (!(var.global_get("backendsFound") == "true")) { //error message if no backends configured
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>Incorrect backend configuration</title>
</head>
<body>
<h1>Incorrect backend configuration</h1>
<p>Please check your deployment. It may not have pods running or Varnish is pointed to a non existing deployment.</p>
<p>XID: "} + req.xid + {"</p>
<hr>
</body>
</html>
"} );
} else { //default error message for the rest of the cases
synthetic( {"<!DOCTYPE html>
<html>
<head>
<title>"} + resp.status + " " + resp.reason + {"</title>
</head>
<body>
<h1>Error "} + resp.status + " " + resp.reason + {"</h1>
<p>"} + resp.reason + {"</p>
<h3>Guru meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"} );
}
return (deliver);
}
sub vcl_hash {
// Called after vcl_recv to create a hash value for the request. This is used as a key
// to look up the object in Varnish.
hash_data(req.url);
return (lookup);
}
sub vcl_hit {
// Do not serve stale objects
if (obj.ttl >= 0s) {
return (deliver);
}
return (restart);
}
sub vcl_backend_response {
set beresp.http.Access-Control-Allow-Origin = "*";
set beresp.http.Access-Control-Allow-Methods = "GET";
# cache everything in /assets folder
if (bereq.url ~ "/assets" ) {
unset beresp.http.set-cookie;
# cache for 2 days
set beresp.http.cache-control = "public, max-age=2880";
set beresp.ttl = 2d;
return (deliver);
}
if (bereq.url ~ "/static") {
unset beresp.http.set-cookie;
# cache for 1 day
set beresp.http.cache-control = "public, max-age=1440";
set beresp.ttl = 1d;
return (deliver);
}
// Do not cache 404s from backends
if (beresp.status == 404) {
set beresp.ttl = 0s;
}
return (deliver);
}
sub vcl_deliver {
set resp.http.grace = req.http.grace;
if (obj.hits > 0) {
set resp.http.X-Varnish-Cache = "HIT";
}
else {
set resp.http.X-Varnish-Cache = "MISS";
}
return (deliver);
}
sub vcl_fini {
return (ok);
}