Skip to content

Commit f97980c

Browse files
committed
Merge pull request #229 from zghosts/feature/vcl
extract vcl into a reusable set of subroutines
2 parents 984a322 + 396519d commit f97980c

26 files changed

+467
-258
lines changed

doc/spelling_word_list.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ whitelisted
1717
paywall
1818
paywalls
1919
github
20+
vcl
21+
fos
22+
Vcl

doc/varnish-configuration.rst

Lines changed: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,41 @@ will be used throughout the Varnish examples on this page.
3333
trigger invalidation are whitelisted here. Otherwise, lost cache invalidation
3434
requests will lead to lots of confusion.
3535

36+
Provided Vcl Subroutines
37+
~~~~~~~~~~~~~~~~~~~~~~~~
38+
39+
In order to ease configuration we provide a set of vcl subroutines in the resources/config directory.
40+
These can be directly included into ``your_varnish.vcl`` and the needed subroutines called
41+
from the respective vcl_* subroutines.
42+
43+
.. important::
44+
When including one of the provided vcl you need to call all the defined subroutines
45+
or your configuration will not be valid.
46+
47+
See the respective sections below on how to configure usage of each of the provided vcl's.
48+
3649
Purge
3750
~~~~~
3851

3952
To configure Varnish for `handling PURGE requests <https://www.varnish-cache.org/docs/3.0/tutorial/purging.html>`_:
4053

4154
Purge removes a specific URL (including query strings) in all its variants (as specified by the ``Vary`` header).
4255

43-
.. configuration-block::
56+
Subroutines are provided in ``resources/config/varnis-[version]/fos_purge.vcl``.
4457

45-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/purge.vcl
46-
:language: varnish4
47-
:linenos:
58+
To enable support add the following to ``your_varnish.vcl``:
59+
60+
.. code-block:: varnish3
4861
49-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/purge.vcl
62+
include "path-to-config/varnish-[version]/fos_purge.vcl";
63+
64+
.. configuration-block::
65+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/fos.vcl
66+
:language: varnish4
67+
:lines: 17,19,21-22
68+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/fos.vcl
5069
:language: varnish3
51-
:linenos:
70+
:lines: 15,17,19-20,25-32
5271

5372
Refresh
5473
~~~~~~~
@@ -58,26 +77,44 @@ add the following to your Varnish configuration:
5877

5978
Refresh invalidates a specific URL including the query string, but *not* its variants.
6079

61-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/refresh.vcl
62-
:language: varnish3
63-
:linenos:
80+
Subroutines are provided in ``fos_refresh.vcl``
81+
82+
To enable support add the following to ``your_varnish.vcl``:
83+
84+
.. code-block:: varnish3
85+
86+
include "path-to-config/varnish-[version]/fos_refresh.vcl";
87+
88+
.. configuration-block::
89+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/fos.vcl
90+
:language: varnish4
91+
:lines: 17,20-22
92+
93+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/fos.vcl
94+
:language: varnish3
95+
:lines: 15,18-20
6496

6597
Ban
6698
~~~
6799

68100
To configure Varnish for `handling BAN requests <https://www.varnish-software.com/static/book/Cache_invalidation.html#banning>`_:
69101

70-
.. configuration-block::
102+
Subroutines are provided in ``fos_ban.vcl``
71103

72-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/ban.vcl
104+
To enable support add the following to ``your_varnish.vcl``:
105+
106+
.. code-block:: varnish3
107+
108+
include "path-to-config/varnish-[version]/fos_ban.vcl";
109+
110+
.. configuration-block::
111+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/fos.vcl
73112
:language: varnish4
74-
:lines: 1-7, 15-18, 20-
75-
:linenos:
113+
:lines: 17-18,21-29
76114

77-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/ban.vcl
115+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/fos.vcl
78116
:language: varnish3
79-
:lines: 1-7, 15-18, 20-
80-
:linenos:
117+
:lines: 15-16,19-24,33-35
81118

82119
Varnish contains a `ban lurker`_ that crawls the content to eventually throw out banned data even when it’s not requested by any client.
83120

@@ -88,23 +125,22 @@ Varnish contains a `ban lurker`_ that crawls the content to eventually throw out
88125
Tagging
89126
~~~~~~~
90127

91-
Add the following to your Varnish configuration to enable :ref:`cache tagging <tags>`.
128+
If you have included fos_ban.vcl, tagging will be automatically enabled using a ``X-Cache-Tags`` header :ref:`cache tagging <tags>`.
92129

93130
.. note::
94-
95-
The custom ``X-Cache-Tags`` header should match the tagging header
96-
:ref:`configured in the cache invalidator <custom_tags_header>`.
131+
If you need to use a different tag for the headers than the default ``X-Cache-Tags`` used in ``fos_ban.vcl``,
132+
you need to write your own VCL code and change the tagging header :ref:`configured in the cache invalidator <custom_tags_header>`.
97133

98134
.. configuration-block::
99135

100-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/ban.vcl
136+
.. literalinclude:: ../resources/config/varnish-4/fos_ban.vcl
101137
:language: varnish4
102-
:emphasize-lines: 8-13,39
138+
:emphasize-lines: 8-13,40-41
103139
:linenos:
104140

105-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/ban.vcl
141+
.. literalinclude:: ../resources/config/varnish-3/fos_ban.vcl
106142
:language: varnish3
107-
:emphasize-lines: 8-13,39
143+
:emphasize-lines: 8-13,40-41
108144
:linenos:
109145

110146
.. _varnish user context:
@@ -115,17 +151,22 @@ User Context
115151
To support :doc:`user context hashing <user-context>` you need to add some logic
116152
to the ``recv`` and the ``deliver`` methods:
117153

118-
.. configuration-block::
154+
Subroutines are provided in ``fos_user_context.vcl``.
155+
156+
To enable support add the following to ``your_varnish.vcl``:
119157

158+
.. code-block:: varnish3
159+
160+
include "path-to-config/varnish-[version]/fos_user_context.vcl";
161+
162+
.. configuration-block::
120163
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/user_context.vcl
121164
:language: varnish4
122-
:lines: 3-29, 33-
123-
:linenos:
165+
:lines: 3-
124166

125167
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/user_context.vcl
126168
:language: varnish3
127-
:lines: 1-27, 31-
128-
:linenos:
169+
:lines: 3-
129170

130171
.. sidebar:: Caching User Specific Content
131172

@@ -205,15 +246,22 @@ Configure your Varnish to set a custom header (`X-Cache`) that shows whether a
205246
cache hit or miss occurred. This header will only be set if your application
206247
sends an `X-Cache-Debug` header:
207248

208-
.. configuration-block::
249+
Subroutines are provided in ``fos_debug.vcl``.
250+
251+
To enable support add the following to ``your_varnish.vcl``:
252+
253+
.. code-block:: varnish3
254+
255+
include "path-to-config/varnish-[version]/fos_debug.vcl";
209256
210-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/debug.vcl
257+
.. configuration-block::
258+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-4/user_context.vcl
211259
:language: varnish4
212-
:linenos:
260+
:lines: 12,13,15
213261

214-
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/debug.vcl
262+
.. literalinclude:: ../tests/Functional/Fixtures/varnish-3/user_context.vcl
215263
:language: varnish3
216-
:linenos:
264+
:lines: 12,13,15
217265

218266
.. _`builtin VCL`: https://www.varnish-cache.org/trac/browser/bin/varnishd/builtin.vcl?rev=4.0
219267
.. _`default VCL`: https://www.varnish-cache.org/trac/browser/bin/varnishd/default.vcl?rev=3.0

tests/Functional/Fixtures/varnish-3/ban.vcl renamed to resources/config/varnish-3/fos_ban.vcl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
sub vcl_recv {
1+
/*
2+
* This file is part of the FOSHttpCache package.
3+
*
4+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
sub fos_ban_recv {
211

312
if (req.request == "BAN") {
413
if (!client.ip ~ invalidators) {
@@ -22,20 +31,22 @@ sub vcl_recv {
2231
}
2332
}
2433

25-
sub vcl_fetch {
34+
sub fos_ban_fetch {
2635

2736
# Set ban-lurker friendly custom headers
2837
set beresp.http.X-Url = req.url;
2938
set beresp.http.X-Host = req.http.host;
3039
}
3140

32-
sub vcl_deliver {
41+
sub fos_ban_deliver {
3342

3443
# Keep ban-lurker headers only if debugging is enabled
3544
if (!resp.http.X-Cache-Debug) {
3645
# Remove ban-lurker friendly custom headers when delivering to client
3746
unset resp.http.X-Url;
3847
unset resp.http.X-Host;
48+
49+
# Unset the tagged cache headers
3950
unset resp.http.X-Cache-Tags;
4051
}
4152
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file is part of the FOSHttpCache package.
3+
*
4+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
sub fos_debug_deliver {
11+
# Add extra headers if debugging is enabled
12+
if (resp.http.X-Cache-Debug) {
13+
if (obj.hits > 0) {
14+
set resp.http.X-Cache = "HIT";
15+
} else {
16+
set resp.http.X-Cache = "MISS";
17+
}
18+
}
19+
}

tests/Functional/Fixtures/varnish-3/purge.vcl renamed to resources/config/varnish-3/fos_purge.vcl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
sub vcl_recv {
1+
/*
2+
* This file is part of the FOSHttpCache package.
3+
*
4+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
sub fos_purge_recv {
211
if (req.request == "PURGE") {
312
if (!client.ip ~ invalidators) {
413
error 405 "Not allowed";
@@ -7,7 +16,7 @@ sub vcl_recv {
716
}
817
}
918

10-
sub vcl_hit {
19+
sub fos_purge_hit {
1120
if (req.request == "PURGE") {
1221
purge;
1322
error 204 "Purged";
@@ -16,7 +25,7 @@ sub vcl_hit {
1625

1726
# The purge in vcl_miss is necessary to purge all variants in the cases where
1827
# you hit an object, but miss a particular variant.
19-
sub vcl_miss {
28+
sub fos_purge_miss {
2029
if (req.request == "PURGE") {
2130
purge;
2231
error 204 "Purged (Not in cache)";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This file is part of the FOSHttpCache package.
3+
*
4+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
sub fos_refresh_recv {
11+
if (req.http.Cache-Control ~ "no-cache" && client.ip ~ invalidators) {
12+
set req.hash_always_miss = true;
13+
}
14+
}

0 commit comments

Comments
 (0)