Skip to content

Commit ad74169

Browse files
committed
feature #4628 Varnish cookbook session cookie handling (dbu)
This PR was merged into the 2.3 branch. Discussion ---------- Varnish cookbook session cookie handling | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | #3881 This builds on top of #4627 but i wanted to keep it separate as there are open questions in here. Commits ------- b294b24 cleanup from feedback 7a4dafc remove part about vary on cookie c88ad32 explain how to work with cookies and sessions when caching
2 parents 3921d70 + b294b24 commit ad74169

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

book/http_cache.rst

+2
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ This has two very reasonable consequences:
383383
blog post). Caching them would prevent certain requests from hitting and
384384
mutating your application.
385385

386+
.. _http-cache-defaults:
387+
386388
Caching Rules and Defaults
387389
~~~~~~~~~~~~~~~~~~~~~~~~~~
388390

cookbook/cache/varnish.rst

+53
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,57 @@ If the ``X-Forwarded-Port`` header is not set correctly, Symfony will append
6060
the port where the PHP application is running when generating absolute URLs,
6161
e.g. ``http://example.com:8080/my/path``.
6262

63+
Cookies and Caching
64+
-------------------
65+
66+
By default, a sane caching proxy does not cache anything when a request is sent
67+
with :ref:`cookies or a basic authentication header<http-cache-introduction>`.
68+
This is because the content of the page is supposed to depend on the cookie
69+
value or authentication header.
70+
71+
If you know for sure that the backend never uses sessions or basic
72+
authentication, have varnish remove the corresponding header from requests to
73+
prevent clients from bypassing the cache. In practice, you will need sessions
74+
at least for some parts of the site, e.g. when using forms with
75+
:ref:`CSRF Protection <forms-csrf>`. In this situation, make sure to only
76+
start a session when actually needed, and clear the session when it is no
77+
longer needed. Alternatively, you can look into :doc:`../cache/form_csrf_caching`.
78+
79+
.. 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
80+
81+
Cookies created in Javascript and used only in the frontend, e.g. when using
82+
Google analytics are nonetheless sent to the server. These cookies are not
83+
relevant for the backend and should not affect the caching decision. Configure
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:
88+
89+
.. code-block:: varnish4
90+
91+
sub vcl_recv {
92+
// Remove all cookies except the session ID.
93+
if (req.http.Cookie) {
94+
set req.http.Cookie = ";" + req.http.Cookie;
95+
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
96+
set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1=");
97+
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
98+
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
99+
100+
if (req.http.Cookie == "") {
101+
// If there are no more cookies, remove the header to get page cached.
102+
remove req.http.Cookie;
103+
}
104+
}
105+
}
106+
107+
.. tip::
108+
109+
If content is not different for every user, but depends on the roles of a
110+
user, a solution is to separate the cache per group. This pattern is
111+
implemented and explained by the FOSHttpCacheBundle_ under the name
112+
`User Context`_.
113+
63114
Ensure Consistent Caching Behaviour
64115
-----------------------------------
65116

@@ -176,8 +227,10 @@ proxy before it has expired, it adds complexity to your caching setup.
176227
.. _`Varnish`: https://www.varnish-cache.org
177228
.. _`Edge Architecture`: http://www.w3.org/TR/edge-arch
178229
.. _`GZIP and Varnish`: https://www.varnish-cache.org/docs/3.0/phk/gzip.html
230+
.. _`Clean the cookies header`: https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies
179231
.. _`Surrogate-Capability Header`: http://www.w3.org/TR/edge-arch
180232
.. _`cache invalidation`: http://tools.ietf.org/html/rfc2616#section-13.10
181233
.. _`FOSHttpCacheBundle`: http://foshttpcachebundle.readthedocs.org/
182234
.. _`default.vcl`: https://www.varnish-cache.org/trac/browser/bin/varnishd/default.vcl?rev=3.0
183235
.. _`builtin.vcl`: https://www.varnish-cache.org/trac/browser/bin/varnishd/builtin.vcl?rev=4.0
236+
.. _`User Context`: http://foshttpcachebundle.readthedocs.org/en/latest/features/user-context.html

0 commit comments

Comments
 (0)