-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.vcl
More file actions
113 lines (95 loc) · 2.2 KB
/
example.vcl
File metadata and controls
113 lines (95 loc) · 2.2 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
vcl 4.1;
import std;
import directors;
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 5s;
.first_byte_timeout = 30s;
.between_bytes_timeout = 5s;
.probe = {
.url = "/health";
.interval = 5s;
.timeout = 2s;
.window = 5;
.threshold = 3;
}
}
backend backend2 {
.host = "192.168.1.100";
.port = "8080";
}
acl purge {
"localhost";
"192.168.0.0"/24;
}
sub vcl_init {
new vdir = directors.round_robin();
vdir.add_backend(default);
vdir.add_backend(backend2);
}
sub vcl_recv {
# Remove cookies for static files
if (req.url ~ "\.(jpg|jpeg|png|gif|css|js|ico)$") {
unset req.http.Cookie;
}
# Pass requests with Authorization header
if (req.http.Authorization) {
return (pass);
}
# Handle PURGE requests
if (req.method == "PURGE") {
if (client.ip !~ purge) {
return (synth(405, "Not allowed"));
}
return (purge);
}
# Use director for load balancing
set req.backend_hint = vdir.backend();
# Normalize Accept-Encoding header
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
unset 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 {
unset req.http.Accept-Encoding;
}
}
return (hash);
}
sub vcl_backend_response {
# Cache static files for 1 hour
if (bereq.url ~ "\.(jpg|jpeg|png|gif|css|js)$") {
set beresp.ttl = 1h;
unset beresp.http.Set-Cookie;
}
# Enable ESI processing
if (beresp.http.Content-Type ~ "text/html") {
set beresp.do_esi = true;
}
return (deliver);
}
sub vcl_deliver {
# Add cache hit/miss header
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
# Remove internal headers
unset resp.http.X-Varnish;
unset resp.http.Via;
unset resp.http.Age;
return (deliver);
}
sub vcl_synth {
if (resp.status == 405) {
set resp.http.Content-Type = "text/plain";
synthetic("Method not allowed");
return (deliver);
}
}