@@ -35,6 +35,7 @@ application:
35
35
.. code-block :: text
36
36
37
37
sub vcl_recv {
38
+ // Add a Surrogate-Capability header to announce ESI support.
38
39
set req.http.Surrogate-Capability = "abc=ESI/1.0";
39
40
}
40
41
@@ -45,12 +46,16 @@ Symfony2 adds automatically:
45
46
.. code-block :: text
46
47
47
48
sub vcl_fetch {
49
+ /*
50
+ Check for ESI acknowledgement
51
+ and remove Surrogate-Control header
52
+ */
48
53
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
49
54
unset beresp.http.Surrogate-Control;
50
55
51
- // for Varnish >= 3.0
56
+ // For Varnish >= 3.0
52
57
set beresp.do_esi = true;
53
- // for Varnish < 3.0
58
+ // For Varnish < 3.0
54
59
// esi;
55
60
}
56
61
}
@@ -75,14 +80,43 @@ that will invalidate the cache for a given resource:
75
80
76
81
.. code-block :: text
77
82
83
+ /*
84
+ Connect to the backend server
85
+ on the local machine on port 8080
86
+ */
87
+ backend default {
88
+ .host = "127.0.0.1";
89
+ .port = "8080";
90
+ }
91
+
92
+ sub vcl_recv {
93
+ /*
94
+ Varnish default behaviour doesn't support PURGE.
95
+ Match the PURGE request and immediately do a cache lookup,
96
+ otherwise Varnish will directly pipe the request to the backend
97
+ and bypass the cache
98
+ */
99
+ if (req.request == "PURGE") {
100
+ return(lookup);
101
+ }
102
+ }
103
+
78
104
sub vcl_hit {
105
+ // Match PURGE request
79
106
if (req.request == "PURGE") {
107
+ // Force object expiration for Varnish < 3.0
80
108
set obj.ttl = 0s;
109
+ // Do an actual purge for Varnish >= 3.0
110
+ // purge;
81
111
error 200 "Purged";
82
112
}
83
113
}
84
114
85
115
sub vcl_miss {
116
+ /*
117
+ Match the PURGE request and
118
+ indicate the request wasn't stored in cache.
119
+ */
86
120
if (req.request == "PURGE") {
87
121
error 404 "Not purged";
88
122
}
@@ -91,7 +125,56 @@ that will invalidate the cache for a given resource:
91
125
.. caution ::
92
126
93
127
You must protect the ``PURGE `` HTTP method somehow to avoid random people
94
- purging your cached data.
128
+ purging your cached data. You can do this by setting up an access list:
129
+
130
+ .. code-block :: text
131
+ /*
132
+ Connect to the backend server
133
+ on the local machine on port 8080
134
+ */
135
+ backend default {
136
+ .host = "127.0.0.1";
137
+ .port = "8080";
138
+ }
139
+
140
+ // Acl's can contain IP's, subnets and hostnames
141
+ acl purge {
142
+ "localhost";
143
+ "192.168.55.0"/24;
144
+ }
145
+
146
+ sub vcl_recv {
147
+ // Match PURGE request to avoid cache bypassing
148
+ if (req.request == "PURGE") {
149
+ // Match client IP to the acl
150
+ if (!client.ip ~ purge) {
151
+ // Deny access
152
+ error 405 "Not allowed.";
153
+ }
154
+ // Perform a cache lookup
155
+ return(lookup);
156
+ }
157
+ }
158
+
159
+ sub vcl_hit {
160
+ // Match PURGE request
161
+ if (req.request == "PURGE") {
162
+ // Force object expiration for Varnish < 3.0
163
+ set obj.ttl = 0s;
164
+ // Do an actual purge for Varnish >= 3.0
165
+ // purge;
166
+ error 200 "Purged";
167
+ }
168
+ }
169
+
170
+ sub vcl_miss {
171
+ // Match PURGE request
172
+ if (req.request == "PURGE") {
173
+ // Indicate that the object isn't stored in cache
174
+ error 404 "Not purged";
175
+ }
176
+ }
177
+
95
178
96
179
.. _`Edge Architecture` : http://www.w3.org/TR/edge-arch
97
180
.. _`GZIP and Varnish` : https://www.varnish-cache.org/docs/3.0/phk/gzip.html
0 commit comments