@@ -15,25 +15,32 @@ cached content fast and including support for :ref:`Edge Side Includes <edge-sid
15
15
Make Symfony Trust the Reverse Proxy
16
16
------------------------------------
17
17
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.
21
26
22
27
.. _varnish-x-forwarded-headers :
23
28
24
29
Routing and X-FORWARDED Headers
25
30
-------------------------------
26
31
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.
30
37
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
32
39
on the default HTTP port 80. For HTTPS connections, there is another proxy
33
40
(as Varnish does not do HTTPS itself) on the default HTTPS port 443 that
34
41
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:
37
44
38
45
.. code-block :: varnish4
39
46
@@ -45,45 +52,30 @@ the following configuration snippet:
45
52
}
46
53
}
47
54
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
-
63
55
Cookies and Caching
64
56
-------------------
65
57
66
58
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 >`.
68
60
This is because the content of the page is supposed to depend on the cookie
69
61
value or authentication header.
70
62
71
63
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
73
65
prevent clients from bypassing the cache. In practice, you will need sessions
74
66
at least for some parts of the site, e.g. when using forms with
75
67
:ref: `CSRF Protection <forms-csrf >`. In this situation, make sure to
76
68
:doc: `only start a session when actually needed </cookbook/session/avoid_session_start >`
77
69
and clear the session when it is no longer needed. Alternatively, you can look
78
70
into :doc: `/cookbook/cache/form_csrf_caching `.
79
71
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
82
74
relevant for the backend and should not affect the caching decision. Configure
83
75
your Varnish cache to `clean the cookies header `_. You want to keep the
84
76
session cookie, if there is one, and get rid of all other cookies so that pages
85
77
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 `` :
87
79
88
80
.. code-block :: varnish4
89
81
@@ -110,8 +102,8 @@ configuration of PHP, your session cookie has the name PHPSESSID:
110
102
implemented and explained by the FOSHttpCacheBundle _ under the name
111
103
`User Context `_.
112
104
113
- Ensure Consistent Caching Behaviour
114
- -----------------------------------
105
+ Ensure Consistent Caching Behavior
106
+ ----------------------------------
115
107
116
108
Varnish uses the cache headers sent by your application to determine how
117
109
to cache content. However, versions prior to Varnish 4 did not respect
@@ -143,7 +135,7 @@ using Varnish 3:
143
135
Enable Edge Side Includes (ESI)
144
136
-------------------------------
145
137
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 >`,
147
139
Symfony detects whether it talks to a reverse proxy that understands ESI or
148
140
not. When you use the Symfony reverse proxy, you don't need to do anything.
149
141
But to make Varnish instead of Symfony resolve the ESI tags, you need some
@@ -168,10 +160,11 @@ application:
168
160
169
161
.. note ::
170
162
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.
173
166
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
175
168
is at least one ESI tag by checking the ``Surrogate-Control `` header that
176
169
Symfony adds automatically:
177
170
0 commit comments