1
+ #
2
+ # Customized VCL file for serving up a Drupal site with multiple back-ends.
3
+ #
4
+ # For more information on this VCL, visit the Lullabot article:
5
+ # http://www.lullabot.com/articles/varnish-multiple-web-servers-drupal
6
+ #
7
+
8
+ # Define the internal network subnet.
9
+ # These are used below to allow internal access to certain files while not
10
+ # allowing access from the public internet.
11
+ acl internal {
12
+ " 127.0.0.1" ;
13
+ }
14
+
15
+ # Define the list of backends (web servers).
16
+ # Port 80 Backend Servers
17
+ backend web1 { .host = " 127.0.0.1" ; .probe = { .url = " /status.php" ; .interval = 5s ; .timeout = 1s ; .window = 5 ;.threshold = 3 ; }}
18
+
19
+ # Define the director that determines how to distribute incoming requests.
20
+ director default_director round-robin {
21
+ { .backend = web1; }
22
+ }
23
+
24
+ # Respond to incoming requests.
25
+ sub vcl_recv {
26
+ set req.backend = default_director;
27
+
28
+ # Use anonymous, cached pages if all backends are down.
29
+ if (!req.backend.healthy ) {
30
+ unset req.http.Cookie ;
31
+ }
32
+
33
+ # Allow the backend to serve up stale content if it is responding slowly.
34
+ set req.grace = 6h ;
35
+
36
+ # Do not cache these paths.
37
+ if (req.url ~ " ^/status\.php$" ||
38
+ req.url ~ " ^/update\.php$" ||
39
+ req.url ~ " ^/ooyala/ping$" ||
40
+ req.url ~ " ^/admin/build/features" ||
41
+ req.url ~ " ^/info/.*$" ||
42
+ req.url ~ " ^/flag/.*$" ||
43
+ req.url ~ " ^.*/ajax/.*$" ||
44
+ req.url ~ " ^.*/ahah/.*$" ) {
45
+ return (pass );
46
+ }
47
+
48
+ # Pipe these paths directly to Apache for streaming.
49
+ if (req.url ~ " ^/admin/content/backup_migrate/export" ) {
50
+ return (pipe );
51
+ }
52
+
53
+ # Do not allow outside access to cron.php or install.php.
54
+ if (req.url ~ " ^/(cron|install)\.php$" && !client.ip ~ internal) {
55
+ # Have Varnish throw the error directly.
56
+ error 404 " Page not found." ;
57
+ # Use a custom error page that you've defined in Drupal at the path "404".
58
+ # set req.url = "/404";
59
+ }
60
+
61
+ # Handle compression correctly. Different browsers send different
62
+ # "Accept-Encoding" headers, even though they mostly all support the same
63
+ # compression mechanisms. By consolidating these compression headers into
64
+ # a consistent format, we can reduce the size of the cache and get more hits.=
65
+ # @see: http:// varnish.projects.linpro.no/wiki/FAQ/Compression
66
+ if (req.http.Accept-Encoding ) {
67
+ if (req.http.Accept-Encoding ~ " gzip" ) {
68
+ # If the browser supports it, we'll use gzip.
69
+ set req.http.Accept-Encoding = " gzip" ;
70
+ }
71
+ else if (req.http.Accept-Encoding ~ " deflate" ) {
72
+ # Next, try deflate if it is supported.
73
+ set req.http.Accept-Encoding = " deflate" ;
74
+ }
75
+ else {
76
+ # Unknown algorithm. Remove it and send unencoded.
77
+ unset req.http.Accept-Encoding ;
78
+ }
79
+ }
80
+
81
+ # Always cache the following file types for all users.
82
+ if (req.url ~ " (?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9\=\.]+)?$" ) {
83
+ unset req.http.Cookie ;
84
+ }
85
+
86
+ # Remove all cookies that Drupal doesn't need to know about. ANY remaining
87
+ # cookie will cause the request to pass-through to Apache. For the most part
88
+ # we always set the NO_CACHE cookie after any POST request, disabling the
89
+ # Varnish cache temporarily. The session cookie allows all authenticated users
90
+ # to pass through as long as they're logged in.
91
+ if (req.http.Cookie ) {
92
+ set req.http.Cookie = " ;" + req.http.Cookie ;
93
+ set req.http.Cookie = regsuball (req.http.Cookie , " ; +" , " ;" );
94
+ set req.http.Cookie = regsuball (req.http.Cookie , " ;(SESS[a-z0-9]+|NO_CACHE)=" , " ; \1=" );
95
+ set req.http.Cookie = regsuball (req.http.Cookie , " ;[^ ][^;]*" , " " );
96
+ set req.http.Cookie = regsuball (req.http.Cookie , " ^[; ]+|[; ]+$" , " " );
97
+
98
+ if (req.http.Cookie == " " ) {
99
+ # If there are no remaining cookies, remove the cookie header. If there
100
+ # aren't any cookie headers, Varnish's default behavior will be to cache
101
+ # the page.
102
+ unset req.http.Cookie ;
103
+ }
104
+ else {
105
+ # If there is any cookies left (a session or NO_CACHE cookie), do not
106
+ # cache the page. Pass it on to Apache directly.
107
+ return (pass );
108
+ }
109
+ }
110
+ }
111
+
112
+ # Routine used to determine the cache key if storing/retrieving a cached page.
113
+ sub vcl_hash {
114
+ # Include cookie in cache hash.
115
+ # This check is unnecessary because we already pass on all cookies.
116
+ # if (req.http.Cookie) {
117
+ # set req.hash += req.http.Cookie;
118
+ # }
119
+ }
120
+
121
+ # Code determining what to do when serving items from the Apache servers.
122
+ sub vcl_fetch {
123
+ # Don't allow static files to set cookies.
124
+ if (req.url ~ " (?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-9\=\.]+)?$" ) {
125
+ # beresp == Back-end response from the web server.
126
+ unset beresp.http.set-cookie ;
127
+ }
128
+
129
+ # Allow items to be stale if needed.
130
+ set beresp.grace = 6h ;
131
+ }
132
+
133
+ # In the event of an error, show friendlier messages.
134
+ sub vcl_error {
135
+ # Redirect to some other URL in the case of a homepage failure.
136
+ #if (req.url ~ "^/?$") {
137
+ # set obj.status = 302;
138
+ # set obj.http.Location = "http://backup.example.com/";
139
+ #}
140
+
141
+ # Otherwise redirect to the homepage, which will likely be in the cache.
142
+ set obj.http.Content-Type = " text/html; charset=utf-8" ;
143
+ synthetic {"
144
+ <html>
145
+ <head>
146
+ <title>Page Unavailable</title>
147
+ <style>
148
+ body { background: #303030; text-align: center; color: white; }
149
+ #page { border: 1px solid #CCC; width: 500px; margin: 100px auto 0; padding: 30px; background: #323232; }
150
+ a, a:link, a:visited { color: #CCC; }
151
+ .error { color: #222; }
152
+ </style>
153
+ </head>
154
+ <body onload="setTimeout(function() { window.location = '/' }, 5000)">
155
+ <div id="page">
156
+ <h1 class="title">Page Unavailable</h1>
157
+ <p>The page you requested is temporarily unavailable.</p>
158
+ <p>We're redirecting you to the <a href="/">homepage</a> in 5 seconds.</p>
159
+ <div class="error">(Error "} + obj.status + " " + obj.response + {" )</div>
160
+ </div>
161
+ </body>
162
+ </html>
163
+ "} ;
164
+ return (deliver );
165
+ }
166
+
167
+ sub vcl_deliver {
168
+ if (obj.hits > 0 ) {
169
+ set resp.http.V-Cache = " HIT" ;
170
+ } else {
171
+ set resp.http.V-Cache = " MISS" ;
172
+ }
173
+ }
0 commit comments