-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.vcl
More file actions
164 lines (140 loc) · 5.93 KB
/
Copy pathdefault.vcl
File metadata and controls
164 lines (140 loc) · 5.93 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
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
# conf
#
# Common/built-in configuration for all sites
# This configuration consists of 3 parts: HEAD, SITE and TAIL.
# The code in the subroutines within each part are concatenated in order.
# HEAD and TAIl contain common configuration. SITE is included from /etc/varnish/site.vcl
# and can contain any site-specific configuration.
vcl 4.0;
import vsthrottle;
import std;
#import urlcode;
# HEAD
sub vcl_recv {
if (req.restarts > 0) { # see https://varnish-cache.org/docs/6.2/whats-new/upgrading-6.2.html
set req.hash_always_miss = true;
}
# Varnish seems to do this automatically
# Add us to X-Forwarded-For. X-Forwarded-For=(client, proxy1, proxy2, ...)
#if (req.http.X-Forwarded-For) {
# set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
#} else {
# set req.http.X-Forwarded-For = client.ip;
#}
# Google Analytics cookies don't inhibit caching
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *(__utm.|_ga|_ga_[^=]+|_gat|_gid)=[^;]+;? *", "\1"); # Remove Google Analytics Cookies
if (req.http.Cookie == "") {
unset req.http.Cookie;
}
}
}
sub vcl_backend_response {
set beresp.do_stream = true;
set beresp.http.x-url = bereq.url;
set beresp.http.x-host = bereq.http.host;
# Fix response protocol if https forwarding is used
if(bereq.http.X-Forwarded-Proto == "https" && regsub(beresp.http.Location, "^http://([^/]+)/.*", "\1") == bereq.http.Host ) {
set beresp.http.Location = regsub(beresp.http.Location, "^http://(.*)", "https://\1");
}
# Do not compress already compressed formats
if (bereq.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
set beresp.do_gzip = false;
}
}
sub vcl_backend_error {
# Errors coming from different origin?
set beresp.http.Access-Control-Allow-Origin = "*"; # allow all + basic authentication with CORS
set beresp.http.Access-Control-Allow-Headers = "X-Requested-With, Content-Type, Authorization";
set beresp.http.Access-Control-Allow-Methods = "HEAD, GET, POST, OPTIONS, PUT, DELETE";
if (beresp.status == 401) {
set beresp.http.WWW-Authenticate = "Basic realm=Restricted";
}
# Process synth(503, reason)
if (beresp.status == 503) {
synthetic({"<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} + beresp.status + " " + beresp.reason + {"</title>
</head>
<body>
<h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
<p>It seems our front-end could not connect to the back-end server ("} + bereq.backend + {") which hosts the service you were trying to access (<a href="http://"} + bereq.http.host + "" + bereq.url + {"">http://"} + bereq.http.host + bereq.url + {"</a>).</p>
<p>This means that the back-end or our service infrastructure may be experiencing problems.</p>
<p>Please try again in a couple of minutes.</p>
</body>
</html>
"});
return(deliver);
}
}
sub vcl_synth {
# Synthetic response origin?
set resp.http.Access-Control-Allow-Origin = "*"; # allow all + basic authentication with CORS
set resp.http.Access-Control-Allow-Headers = "X-Requested-With, Content-Type, Authorization";
set resp.http.Access-Control-Allow-Methods = "HEAD, GET, POST, OPTIONS, PUT, DELETE";
# Process synth(401, reason)
if (resp.status == 401) {
set resp.http.WWW-Authenticate = "Basic realm=Restricted";
}
# Process synth(200, reason)
if (resp.status == 200) {
set resp.status = 200;
set resp.http.Allow = "HEAD, GET, POST, OPTIONS, PUT, DELETE";
set resp.reason = "OK";
return (deliver);
}
# Process synth(301, reason)
if (resp.status == 301) {
set resp.http.Location = resp.reason;
set resp.status = 301;
set resp.reason = "Moved Permanently";
return (deliver);
}
# Process synth(302, reason)
if (resp.status == 302) {
set resp.http.Location = resp.reason;
set resp.status = 302;
set resp.reason = "Found";
return (deliver);
}
# Process synth(303, reason)
if (resp.status == 303) {
set resp.http.Location = resp.reason;
set resp.status = 303;
set resp.reason = "See Other";
return (deliver);
}
set resp.http.Content-Type = "text/html; charset=utf-8";
}
sub vcl_hit {
if (req.http.Cache-Control ~ "no-cache") {
# Ignore requests via proxy caches, IE users and badly behaved crawlers
# like msnbot that send no-cache with every request.
if (! (req.http.Via || req.http.User-Agent ~ "bot|MSIE")) {
#return (miss);
return (restart); # see https://varnish-cache.org/docs/6.2/whats-new/upgrading-6.2.html
}
}
}
sub vcl_deliver {
if(obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
# SITE SPECIFIC
# Any site-specific configuration can be provided in /etc/varnish/site.vcl
# Or if needed, this file (/etc/varnish/default.vcl) could be overridden as well.
include "site.vcl";
# TAIL
# If something needs to be added after the site-specific configuration, add it here
sub vcl_recv {}
sub vcl_backend_response {}
sub vcl_backend_error {}
sub vcl_synth {}
sub vcl_hit {}
sub vcl_deliver {}