Skip to content

Commit 8823790

Browse files
committed
MC-18028: Varnish 6.2 with 2.3-develop export config for varnish 5 causes error. Fixed
1 parent ede25ae commit 8823790

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# VCL version 5.0 is not supported so it should be 4.0 even though actually used Varnish version is 6
2+
vcl 4.0;
3+
4+
import std;
5+
# The minimal Varnish version is 6.0
6+
# For SSL offloading, pass the following header in your proxy server or load balancer: '/* {{ ssl_offloaded_header }} */: https'
7+
8+
backend default {
9+
.host = "/* {{ host }} */";
10+
.port = "/* {{ port }} */";
11+
.first_byte_timeout = 600s;
12+
.probe = {
13+
.url = "/pub/health_check.php";
14+
.timeout = 2s;
15+
.interval = 5s;
16+
.window = 10;
17+
.threshold = 5;
18+
}
19+
}
20+
21+
acl purge {
22+
/* {{ ips }} */
23+
}
24+
25+
sub vcl_recv {
26+
if (req.method == "PURGE") {
27+
if (client.ip !~ purge) {
28+
return (synth(405, "Method not allowed"));
29+
}
30+
# To use the X-Pool header for purging varnish during automated deployments, make sure the X-Pool header
31+
# has been added to the response in your backend server config. This is used, for example, by the
32+
# capistrano-magento2 gem for purging old content from varnish during it's deploy routine.
33+
if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
34+
return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));
35+
}
36+
if (req.http.X-Magento-Tags-Pattern) {
37+
ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
38+
}
39+
if (req.http.X-Pool) {
40+
ban("obj.http.X-Pool ~ " + req.http.X-Pool);
41+
}
42+
return (synth(200, "Purged"));
43+
}
44+
45+
if (req.method != "GET" &&
46+
req.method != "HEAD" &&
47+
req.method != "PUT" &&
48+
req.method != "POST" &&
49+
req.method != "TRACE" &&
50+
req.method != "OPTIONS" &&
51+
req.method != "DELETE") {
52+
/* Non-RFC2616 or CONNECT which is weird. */
53+
return (pipe);
54+
}
55+
56+
# We only deal with GET and HEAD by default
57+
if (req.method != "GET" && req.method != "HEAD") {
58+
return (pass);
59+
}
60+
61+
# Bypass shopping cart, checkout and search requests
62+
if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") {
63+
return (pass);
64+
}
65+
66+
# Bypass health check requests
67+
if (req.url ~ "/pub/health_check.php") {
68+
return (pass);
69+
}
70+
71+
# Set initial grace period usage status
72+
set req.http.grace = "none";
73+
74+
# normalize url in case of leading HTTP scheme and domain
75+
set req.url = regsub(req.url, "^http[s]?://", "");
76+
77+
# collect all cookies
78+
std.collect(req.http.Cookie);
79+
80+
# Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
81+
if (req.http.Accept-Encoding) {
82+
if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
83+
# No point in compressing these
84+
unset req.http.Accept-Encoding;
85+
} elsif (req.http.Accept-Encoding ~ "gzip") {
86+
set req.http.Accept-Encoding = "gzip";
87+
} elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
88+
set req.http.Accept-Encoding = "deflate";
89+
} else {
90+
# unknown algorithm
91+
unset req.http.Accept-Encoding;
92+
}
93+
}
94+
95+
# Remove all marketing get parameters to minimize the cache objects
96+
if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=") {
97+
set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=[-_A-z0-9+()%.]+&?", "");
98+
set req.url = regsub(req.url, "[?|&]+$", "");
99+
}
100+
101+
# Static files caching
102+
if (req.url ~ "^/(pub/)?(media|static)/") {
103+
# Static files should not be cached by default
104+
return (pass);
105+
106+
# But if you use a few locales and don't use CDN you can enable caching static files by commenting previous line (#return (pass);) and uncommenting next 3 lines
107+
#unset req.http.Https;
108+
#unset req.http./* {{ ssl_offloaded_header }} */;
109+
#unset req.http.Cookie;
110+
}
111+
112+
return (hash);
113+
}
114+
115+
sub vcl_hash {
116+
if (req.http.cookie ~ "X-Magento-Vary=") {
117+
hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
118+
}
119+
120+
# For multi site configurations to not cache each other's content
121+
if (req.http.host) {
122+
hash_data(req.http.host);
123+
} else {
124+
hash_data(server.ip);
125+
}
126+
127+
# To make sure http users don't see ssl warning
128+
if (req.http./* {{ ssl_offloaded_header }} */) {
129+
hash_data(req.http./* {{ ssl_offloaded_header }} */);
130+
}
131+
/* {{ design_exceptions_code }} */
132+
133+
if (req.url ~ "/graphql") {
134+
call process_graphql_headers;
135+
}
136+
}
137+
138+
sub process_graphql_headers {
139+
if (req.http.Store) {
140+
hash_data(req.http.Store);
141+
}
142+
if (req.http.Content-Currency) {
143+
hash_data(req.http.Content-Currency);
144+
}
145+
}
146+
147+
sub vcl_backend_response {
148+
149+
set beresp.grace = 3d;
150+
151+
if (beresp.http.content-type ~ "text") {
152+
set beresp.do_esi = true;
153+
}
154+
155+
if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
156+
set beresp.do_gzip = true;
157+
}
158+
159+
if (beresp.http.X-Magento-Debug) {
160+
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
161+
}
162+
163+
# cache only successfully responses and 404s
164+
if (beresp.status != 200 && beresp.status != 404) {
165+
set beresp.ttl = 0s;
166+
set beresp.uncacheable = true;
167+
return (deliver);
168+
} elsif (beresp.http.Cache-Control ~ "private") {
169+
set beresp.uncacheable = true;
170+
set beresp.ttl = 86400s;
171+
return (deliver);
172+
}
173+
174+
# validate if we need to cache it and prevent from setting cookie
175+
if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
176+
unset beresp.http.set-cookie;
177+
}
178+
179+
# If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
180+
if (beresp.ttl <= 0s ||
181+
beresp.http.Surrogate-control ~ "no-store" ||
182+
(!beresp.http.Surrogate-Control &&
183+
beresp.http.Cache-Control ~ "no-cache|no-store") ||
184+
beresp.http.Vary == "*") {
185+
# Mark as Hit-For-Pass for the next 2 minutes
186+
set beresp.ttl = 120s;
187+
set beresp.uncacheable = true;
188+
}
189+
190+
return (deliver);
191+
}
192+
193+
sub vcl_deliver {
194+
if (resp.http.X-Magento-Debug) {
195+
if (resp.http.x-varnish ~ " ") {
196+
set resp.http.X-Magento-Cache-Debug = "HIT";
197+
set resp.http.Grace = req.http.grace;
198+
} else {
199+
set resp.http.X-Magento-Cache-Debug = "MISS";
200+
}
201+
} else {
202+
unset resp.http.Age;
203+
}
204+
205+
# Not letting browser to cache non-static files.
206+
if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
207+
set resp.http.Pragma = "no-cache";
208+
set resp.http.Expires = "-1";
209+
set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
210+
}
211+
212+
unset resp.http.X-Magento-Debug;
213+
unset resp.http.X-Magento-Tags;
214+
unset resp.http.X-Powered-By;
215+
unset resp.http.Server;
216+
unset resp.http.X-Varnish;
217+
unset resp.http.Via;
218+
unset resp.http.Link;
219+
}
220+
221+
sub vcl_hit {
222+
if (obj.ttl >= 0s) {
223+
# Hit within TTL period
224+
return (deliver);
225+
}
226+
if (std.healthy(req.backend_hint)) {
227+
if (obj.ttl + /* {{ grace_period }} */s > 0s) {
228+
# Hit after TTL expiration, but within grace period
229+
set req.http.grace = "normal (healthy server)";
230+
return (deliver);
231+
} else {
232+
# Hit after TTL and grace expiration
233+
return (restart);
234+
}
235+
} else {
236+
# server is not healthy, retrieve from cache
237+
set req.http.grace = "unlimited (unhealthy server)";
238+
return (deliver);
239+
}
240+
}

0 commit comments

Comments
 (0)