Skip to content

Commit b294b24

Browse files
committed
cleanup from feedback
* link user context to relevant section of the doc * better explain what we achieve with cleaning the cookies
1 parent 7a4dafc commit b294b24

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

cookbook/cache/varnish.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,22 @@ prevent clients from bypassing the cache. In practice, you will need sessions
7474
at least for some parts of the site, e.g. when using forms with
7575
:ref:`CSRF Protection <forms-csrf>`. In this situation, make sure to only
7676
start a session when actually needed, and clear the session when it is no
77-
longer needed.
77+
longer needed. Alternatively, you can look into :doc:`../cache/form_csrf_caching`.
7878

79-
.. todo link "CSRF Protection" to https://github.com/symfony/symfony-docs/pull/4141
8079
.. todo link "only start a session when actually needed" to cookbook/session/avoid_session_start once https://github.com/symfony/symfony-docs/pull/4661 is merged
8180
8281
Cookies created in Javascript and used only in the frontend, e.g. when using
8382
Google analytics are nonetheless sent to the server. These cookies are not
8483
relevant for the backend and should not affect the caching decision. Configure
85-
your Varnish cache to `clean the cookies header`_. Unless you changed the
86-
default configuration of PHP, your session cookie has the name PHPSESSID:
84+
your Varnish cache to `clean the cookies header`_. You want to keep the
85+
session cookie, if there is one, and get rid of all other cookies so that pages
86+
are cached if there is no active session. Unless you changed the default
87+
configuration of PHP, your session cookie has the name PHPSESSID:
8788

8889
.. code-block:: varnish4
8990
9091
sub vcl_recv {
92+
// Remove all cookies except the session ID.
9193
if (req.http.Cookie) {
9294
set req.http.Cookie = ";" + req.http.Cookie;
9395
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
@@ -96,6 +98,7 @@ default configuration of PHP, your session cookie has the name PHPSESSID:
9698
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
9799
98100
if (req.http.Cookie == "") {
101+
// If there are no more cookies, remove the header to get page cached.
99102
remove req.http.Cookie;
100103
}
101104
}
@@ -106,7 +109,7 @@ default configuration of PHP, your session cookie has the name PHPSESSID:
106109
If content is not different for every user, but depends on the roles of a
107110
user, a solution is to separate the cache per group. This pattern is
108111
implemented and explained by the FOSHttpCacheBundle_ under the name
109-
*User Context*.
112+
`User Context`_.
110113

111114
Ensure Consistent Caching Behaviour
112115
-----------------------------------
@@ -221,3 +224,4 @@ proxy before it has expired, it adds complexity to your caching setup.
221224
.. _`Surrogate-Capability Header`: http://www.w3.org/TR/edge-arch
222225
.. _`cache invalidation`: http://tools.ietf.org/html/rfc2616#section-13.10
223226
.. _`FOSHttpCacheBundle`: http://foshttpcachebundle.readthedocs.org/
227+
.. _`User Context`: http://foshttpcachebundle.readthedocs.org/en/latest/features/user-context.html

0 commit comments

Comments
 (0)