-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.vcl
132 lines (111 loc) · 3.92 KB
/
example.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
vcl 4.0;
# Varnish VMODs, siehe https://varnish-cache.org/vmods/
import std;
# You should specify here all your app nodes and use round robin to select a backend
#include "vcl/config/backends.vcl";
backend default {
.host = "127.0.0.1";
.port = "8000";
}
# ACL for purgers IP. (This needs to contain app server ips)
#include "vcl/config/purge.vcl";
acl purgers {
"127.0.0.1";
"localhost";
"::1";
# Add other IPs here from which you want to be able to flush, especially your Shopware instance
# if Varnish is on another host
}
sub vcl_recv {
# Handle BAN
if (req.method == "BAN") {
if (!std.ip(req.http.X-Real-IP, "0.0.0.0") ~ purgers) {
return (synth(405, "Method not allowed"));
}
if (!req.http.X-Url-Regex && !req.http.X-Cache-Tags) {
# Purge direct url
return (purge);
}
if (req.http.X-Cache-Tags == "all"){
# Possibility to ban all
ban("obj.http.X-Cache-Tags ~ .* && req.http.host == " + req.http.host);
return (synth(200, "BAN URLs for all done."));
}
if (req.http.X-Cache-Tags) {
# Logic for the ban, using the X-Cache-Tags header.
ban("obj.http.X-Cache-Tags ~ " + req.http.X-Cache-Tags + " && req.http.host == " + req.http.host);
return (synth(200, "BAN URLs containing cache tags (" + req.http.X-Cache-Tags + ") done."));
}
# Ban via Regex
ban("req.url ~ " + req.http.X-Url-Regex + " && req.http.host == " + req.http.host);
return (synth(200, "BAN URLs containing (" + req.http.X-Url-Regex + ") done."));
}
# We only deal with GET and HEAD by default
if (req.method != "GET" && req.method != "HEAD") {
return (pass);
}
# Always pass these paths directly to node without caching, add routes that should not be cached
if (req.url ~ "^/(checkout|order-success|payment-failure|account|cart|store-api)(/.*)?$") {
return (pass);
}
return (hash);
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
sub vcl_hit {
}
sub vcl_backend_response {
# Fix Vary Header in some cases
# https://www.varnish-cache.org/trac/wiki/VCLExampleFixupVary
if (beresp.http.Vary ~ "User-Agent") {
set beresp.http.Vary = regsub(beresp.http.Vary, ",? *User-Agent *", "");
set beresp.http.Vary = regsub(beresp.http.Vary, "^, *", "");
if (beresp.http.Vary == "") {
unset beresp.http.Vary;
}
}
# Respect the Cache-Control=private header from the backend
if (
beresp.http.Pragma ~ "no-cache" ||
beresp.http.Cache-Control ~ "no-cache" ||
beresp.http.Cache-Control ~ "private"
) {
set beresp.ttl = 0s;
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
set beresp.uncacheable = true;
return (deliver);
}
# strip the cookie before the image is inserted into cache.
if (bereq.url ~ "\.(png|gif|jpg|swf|css|js|webp)$") {
unset beresp.http.set-cookie;
}
# Allow items to be stale if needed.
set beresp.ttl = 24h;
set beresp.grace = 6h;
# Save the bereq.url so bans work efficiently
set beresp.http.x-url = bereq.url;
set beresp.http.X-Cacheable = "YES";
return (deliver);
}
sub vcl_deliver {
## we don't want the client to cache
set resp.http.Cache-Control = "max-age=0, private";
# Set a cache header to allow us to inspect the response headers during testing
if (obj.hits > 0) {
unset resp.http.set-cookie;
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
# Cache Tags header should not be delivered as it is unneccessary for the client
unset resp.http.X-Cache-Tags;
unset resp.http.X-Cache-Tags-Number;
set resp.http.X-Cache-Hits = obj.hits;
}