Skip to content

Commit f11fe4f

Browse files
committed
Reviewed Cache cookbook articles
1 parent 51af15d commit f11fe4f

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

cookbook/cache/form_csrf_caching.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ validation when submitting the form.
2222

2323
In fact, many reverse proxies (like Varnish) will refuse to cache a page
2424
with a CSRF token. This is because a cookie is sent in order to preserve
25-
the PHP session open and Varnish's default behaviour is to not cache HTTP
25+
the PHP session open and Varnish's default behavior is to not cache HTTP
2626
requests with cookies.
2727

2828
How to Cache Most of the Page and still be able to Use CSRF Protection

cookbook/cache/varnish.rst

+28-35
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,32 @@ cached content fast and including support for :ref:`Edge Side Includes <edge-sid
1515
Make Symfony Trust the Reverse Proxy
1616
------------------------------------
1717

18-
For ESI to work correctly and for the :ref:`X-FORWARDED <varnish-x-forwarded-headers>`
19-
headers to be used, you need to configure Varnish as a
20-
:doc:`trusted proxy </cookbook/request/load_balancer_reverse_proxy>`.
18+
Varnish automatically forwards the IP as ``X-Forwarded-For`` and leaves the
19+
``X-Forwarded-Proto`` header in the request. If you do not configure Varnish as
20+
trusted proxy, Symfony will see all requests as coming through insecure HTTP
21+
connections from the Varnish host instead of the real client.
22+
23+
Remember to configure :ref:`framework.trusted_proxies <reference-framework-trusted-proxies>`
24+
in the Symfony configuration so that Varnish is seen as a trusted proxy and the
25+
:ref:`X-Forwarded <varnish-x-forwarded-headers>` headers are used.
2126

2227
.. _varnish-x-forwarded-headers:
2328

2429
Routing and X-FORWARDED Headers
2530
-------------------------------
2631

27-
To ensure that the Symfony Router generates URLs correctly with Varnish,
28-
a ``X-Forwarded-Port`` header must be present for Symfony to use the
29-
correct port number.
32+
If the ``X-Forwarded-Port`` header is not set correctly, Symfony will append
33+
the port where the PHP application is running when generating absolute URLs,
34+
e.g. ``http://example.com:8080/my/path``. To ensure that the Symfony router
35+
generates URLs correctly with Varnish, add the correct port number in the
36+
``X-Forwarded-Port`` header.
3037

31-
This port depends on your setup. Lets say that external connections come in
38+
This port depends on your setup. Let's say that external connections come in
3239
on the default HTTP port 80. For HTTPS connections, there is another proxy
3340
(as Varnish does not do HTTPS itself) on the default HTTPS port 443 that
3441
handles the SSL termination and forwards the requests as HTTP requests to
35-
Varnish with a ``X-Forwarded-Proto`` header. In this case, you need to add
36-
the following configuration snippet:
42+
Varnish with a ``X-Forwarded-Proto`` header. In this case, add the following to
43+
your Varnish configuration:
3744

3845
.. code-block:: varnish4
3946
@@ -45,45 +52,30 @@ the following configuration snippet:
4552
}
4653
}
4754
48-
.. note::
49-
50-
Remember to configure :ref:`framework.trusted_proxies <reference-framework-trusted-proxies>`
51-
in the Symfony configuration so that Varnish is seen as a trusted proxy
52-
and the ``X-Forwarded-*`` headers are used.
53-
54-
Varnish automatically forwards the IP as ``X-Forwarded-For`` and leaves
55-
the ``X-Forwarded-Proto`` header in the request. If you do not configure
56-
Varnish as trusted proxy, Symfony will see all requests as coming through
57-
insecure HTTP connections from the Varnish host instead of the real client.
58-
59-
If the ``X-Forwarded-Port`` header is not set correctly, Symfony will append
60-
the port where the PHP application is running when generating absolute URLs,
61-
e.g. ``http://example.com:8080/my/path``.
62-
6355
Cookies and Caching
6456
-------------------
6557

6658
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>`.
59+
with :ref:`cookies or a basic authentication header <http-cache-introduction>`.
6860
This is because the content of the page is supposed to depend on the cookie
6961
value or authentication header.
7062

7163
If you know for sure that the backend never uses sessions or basic
72-
authentication, have varnish remove the corresponding header from requests to
64+
authentication, have Varnish remove the corresponding header from requests to
7365
prevent clients from bypassing the cache. In practice, you will need sessions
7466
at least for some parts of the site, e.g. when using forms with
7567
:ref:`CSRF Protection <forms-csrf>`. In this situation, make sure to
7668
:doc:`only start a session when actually needed </cookbook/session/avoid_session_start>`
7769
and clear the session when it is no longer needed. Alternatively, you can look
7870
into :doc:`/cookbook/cache/form_csrf_caching`.
7971

80-
Cookies created in Javascript and used only in the frontend, e.g. when using
81-
Google analytics are nonetheless sent to the server. These cookies are not
72+
Cookies created in JavaScript and used only in the frontend, e.g. when using
73+
Google Analytics, are nonetheless sent to the server. These cookies are not
8274
relevant for the backend and should not affect the caching decision. Configure
8375
your Varnish cache to `clean the cookies header`_. You want to keep the
8476
session cookie, if there is one, and get rid of all other cookies so that pages
8577
are cached if there is no active session. Unless you changed the default
86-
configuration of PHP, your session cookie has the name PHPSESSID:
78+
configuration of PHP, your session cookie has the name ``PHPSESSID``:
8779

8880
.. code-block:: varnish4
8981
@@ -110,8 +102,8 @@ configuration of PHP, your session cookie has the name PHPSESSID:
110102
implemented and explained by the FOSHttpCacheBundle_ under the name
111103
`User Context`_.
112104

113-
Ensure Consistent Caching Behaviour
114-
-----------------------------------
105+
Ensure Consistent Caching Behavior
106+
----------------------------------
115107

116108
Varnish uses the cache headers sent by your application to determine how
117109
to cache content. However, versions prior to Varnish 4 did not respect
@@ -143,7 +135,7 @@ using Varnish 3:
143135
Enable Edge Side Includes (ESI)
144136
-------------------------------
145137

146-
As explained in the :ref:`Edge Side Includes section<edge-side-includes>`,
138+
As explained in the :ref:`Edge Side Includes section <edge-side-includes>`,
147139
Symfony detects whether it talks to a reverse proxy that understands ESI or
148140
not. When you use the Symfony reverse proxy, you don't need to do anything.
149141
But to make Varnish instead of Symfony resolve the ESI tags, you need some
@@ -168,10 +160,11 @@ application:
168160
169161
.. note::
170162

171-
The ``abc`` part of the header isn't important unless you have multiple "surrogates"
172-
that need to advertise their capabilities. See `Surrogate-Capability Header`_ for details.
163+
The ``abc`` part of the header isn't important unless you have multiple
164+
"surrogates" that need to advertise their capabilities. See
165+
`Surrogate-Capability Header`_ for details.
173166

174-
Then, optimize Varnish so that it only parses the Response contents when there
167+
Then, optimize Varnish so that it only parses the response contents when there
175168
is at least one ESI tag by checking the ``Surrogate-Control`` header that
176169
Symfony adds automatically:
177170

0 commit comments

Comments
 (0)