You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cookbook/cache/varnish.rst
+9-5
Original file line number
Diff line number
Diff line change
@@ -74,20 +74,22 @@ prevent clients from bypassing the cache. In practice, you will need sessions
74
74
at least for some parts of the site, e.g. when using forms with
75
75
:ref:`CSRF Protection <forms-csrf>`. In this situation, make sure to only
76
76
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`.
78
78
79
-
.. todo link "CSRF Protection" to https://github.com/symfony/symfony-docs/pull/4141
80
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
81
80
82
81
Cookies created in Javascript and used only in the frontend, e.g. when using
83
82
Google analytics are nonetheless sent to the server. These cookies are not
84
83
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:
87
88
88
89
.. code-block:: varnish4
89
90
90
91
sub vcl_recv {
92
+
// Remove all cookies except the session ID.
91
93
if (req.http.Cookie) {
92
94
set req.http.Cookie = ";" + req.http.Cookie;
93
95
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
@@ -96,6 +98,7 @@ default configuration of PHP, your session cookie has the name PHPSESSID:
96
98
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
97
99
98
100
if (req.http.Cookie == "") {
101
+
// If there are no more cookies, remove the header to get page cached.
99
102
remove req.http.Cookie;
100
103
}
101
104
}
@@ -106,7 +109,7 @@ default configuration of PHP, your session cookie has the name PHPSESSID:
106
109
If content is not different for every user, but depends on the roles of a
107
110
user, a solution is to separate the cache per group. This pattern is
108
111
implemented and explained by the FOSHttpCacheBundle_ under the name
109
-
*User Context*.
112
+
`User Context`_.
110
113
111
114
Ensure Consistent Caching Behaviour
112
115
-----------------------------------
@@ -221,3 +224,4 @@ proxy before it has expired, it adds complexity to your caching setup.
0 commit comments