-
Notifications
You must be signed in to change notification settings - Fork 2
/
varnish.vcl
95 lines (80 loc) · 2.06 KB
/
varnish.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
vcl 4.1;
import std;
import directors;
import vsthrottle;
probe chaosprobe {
.url = "/status";
.timeout = 1s;
.interval = 2s;
}
backend chaos1 {
.host = "chaosbackend:8180";
.probe = chaosprobe;
.between_bytes_timeout = 2s;
.first_byte_timeout = 2s;
.connect_timeout = 1s;
}
backend chaos2 {
.host = "chaosbackend:8181";
.probe = chaosprobe;
.between_bytes_timeout = 2s;
.first_byte_timeout = 2s;
.connect_timeout = 1s;
}
sub vcl_init {
new chaos = directors.round_robin();
chaos.add_backend(chaos1);
chaos.add_backend(chaos2);
}
sub vcl_recv {
unset req.http.cookie;
if (req.url ~ "^/foo") {
set req.backend_hint = chaos1;
} else if(req.url ~ "^/bar") {
set req.backend_hint = chaos2;
} else {
set req.backend_hint = chaos.backend();
}
set req.http.X-Varnish-Director = req.backend_hint;
}
sub vcl_hit {
set req.http.X-Varnish-Cache = "HIT";
if (obj.ttl <= 0s && obj.grace > 0s) {
set req.http.x-cache = "STALE";
}
}
sub vcl_miss {
set req.http.X-Varnish-Cache = "MISS";
if (vsthrottle.is_denied("apikey:" + client.ip, 1000,10s,30s)) {
set req.http.X-Varnish-Cache = "THROTTLED";
return (synth(429, "Throttling Backend"));
}
}
sub vcl_pass {
set req.http.X-Varnish-Cache = "PASS";
}
sub vcl_pipe {
set req.http.X-Varnish-Cache = "PIPE";
}
sub vcl_synth {
set resp.http.X-Varnish-Cache = "SYNTH";
}
sub vcl_backend_fetch {
}
sub vcl_backend_response {
set beresp.http.Varnish-Backend = beresp.backend;
}
sub vcl_backend_error {
set beresp.http.X-Varnish-Cache = "BACKEND_ERROR";
}
sub vcl_deliver {
if (obj.uncacheable) {
set resp.http.X-Varnish-Cache = resp.http.X-Varnish-Cache + " uncacheable" ;
}
if (!resp.http.X-Varnish-Cache) {
set resp.http.X-Varnish-Cache = req.http.X-Varnish-Cache;
}
set resp.http.X-Varnish-Hits = obj.hits;
set resp.http.X-Varnish-Director = req.http.X-Varnish-Director;
std.log("prom=backends backend=" + resp.http.X-Varnish-Backend + ",director="+ resp.http.X-Varnish-Director + ",cache=" + req.http.X-Varnish-Cache + ",status=" + resp.status +",desc=vcl_deliver");
}