diff --git a/cookbook/cache/varnish.rst b/cookbook/cache/varnish.rst index 39b686872f5..1be9019abb0 100644 --- a/cookbook/cache/varnish.rst +++ b/cookbook/cache/varnish.rst @@ -75,23 +75,43 @@ session cookie, if there is one, and get rid of all other cookies so that pages are cached if there is no active session. Unless you changed the default configuration of PHP, your session cookie has the name ``PHPSESSID``: -.. code-block:: varnish4 +.. configuration-block:: - sub vcl_recv { - // Remove all cookies except the session ID. - if (req.http.Cookie) { - set req.http.Cookie = ";" + req.http.Cookie; - set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); - set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1="); - set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); - set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); - - if (req.http.Cookie == "") { - // If there are no more cookies, remove the header to get page cached. - remove req.http.Cookie; + .. code-block:: varnish4 + + sub vcl_backend_response { + // Remove all cookies except the session ID. + if (req.http.Cookie) { + set req.http.Cookie = ";" + req.http.Cookie; + set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); + set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1="); + set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); + set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); + + if (req.http.Cookie == "") { + // If there are no more cookies, remove the header to get page cached. + unset req.http.Cookie; + } + } + } + + .. code-block:: varnish3 + + sub vcl_fetch { + // Remove all cookies except the session ID. + if (req.http.Cookie) { + set req.http.Cookie = ";" + req.http.Cookie; + set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); + set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1="); + set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); + set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); + + if (req.http.Cookie == "") { + // If there are no more cookies, remove the header to get page cached. + remove req.http.Cookie; + } } } - } .. tip::