1
- Caching Headers
2
- ===============
1
+ cache_control
2
+ =============
3
3
4
4
The configuration contains a number of *rules *. When a request matches the
5
5
parameters described in the ``match `` section, the headers as defined under
6
- ``headers `` will be set on its response.
7
-
8
- The match patterns are applied in the order specified, where the first match
9
- wins:
6
+ ``headers `` will be set on the response, if they are not already set.
10
7
11
8
.. code-block :: yaml
12
9
@@ -40,13 +37,19 @@ wins:
40
37
# match everything to set defaults
41
38
-
42
39
match :
43
- path : ^/
40
+ path : ^/
44
41
headers :
45
42
cache_control : { public: true, max_age: 15, s_maxage: 30, last_modified: "-1 hour" }
46
43
47
44
rules
48
45
-----
49
46
47
+ **type **: ``array ``
48
+
49
+ A set of cache control rules.
50
+
51
+ .. include :: /includes/match.rst
52
+
50
53
.. sidebar :: Merging headers
51
54
52
55
If the response already has certain cache directives set, they are not
@@ -68,113 +71,10 @@ rules
68
71
service ``fos_http_cache.event_listener.cache_control `` and calling
69
72
``setSkip() `` on it. If this method is called, no cache rules are applied.
70
73
71
- The configuration contains a number of *rules *. When a request matches the
72
- parameters described in the ``match `` section, the headers as defined under
73
- ``headers `` will be set on the response, if they are not already set.
74
-
75
- The match patterns are applied in the order specified, where the first match
76
- wins.
77
-
78
- .. _match :
79
-
80
- match
81
- -----
82
-
83
- The headers are only set if all of these are true:
84
-
85
- * the request matches *all * criteria defined under ``match ``
86
- * the request is :term: `safe ` (GET or HEAD)
87
- * the response is considered :term: `cacheable ` (override with
88
- :ref: `additional_cacheable_status ` and :ref: `match_response `).
89
-
90
- All matching criteria are regular expressions.
91
-
92
- path
93
- ~~~~
94
-
95
- For example, ``path: ^/ `` will match every request. To only match the home
96
- page, use ``path: ^/$ ``.
97
-
98
- host
99
- ~~~~
100
-
101
- A regular expression to limit the caching rules to specific hosts, when you
102
- serve more than one host from your Symfony application.
103
-
104
- .. tip ::
105
-
106
- To simplify caching of a site that at the same time has frontend
107
- editing, put the editing on a separate (sub-)domain. Then define a first
108
- rule matching that domain with ``host `` and set ``max-age: 0 `` to make sure
109
- your caching proxy never caches the editing domain.
110
-
111
- methods
112
- ~~~~~~~
113
-
114
- Can be used to limit caching rules to specific HTTP methods like GET requests.
115
-
116
- Note that cache headers are not applied to methods not considered *safe *, not
117
- even when the methods are listed in this configuration.
118
-
119
- ips
120
- ~~~
121
-
122
- An array that can be used to limit the rules to a specified set of request
123
- client IP addresses.
124
-
125
- .. note ::
126
-
127
- If you use a caching proxy and want specific IPs to see different headers,
128
- you need to forward the client IP to the backend. Otherwise, the backend
129
- only sees the caching proxy IP. See `Trusting Proxies `_ in the Symfony
130
- documentation.
131
-
132
- attributes
133
- ~~~~~~~~~~
134
-
135
- An array to filter on route attributes. the most common use case would be
136
- ``_controller `` when you need caching rules applied to a controller. Note that
137
- this is the controller name used in the route, so it depends on your route
138
- configuration whether you need ``Bundle:Name:action `` or
139
- ``service.id:methodName `` (if you defined your `controllers as services `_).
140
-
141
- Note that even for the request attributes, your criteria are interpreted as
142
- regular expressions.
143
-
144
- .. _additional_cacheable_status :
145
-
146
- additional_cacheable_status
147
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
148
-
149
- A list of additional HTTP status codes of the response for which to also apply
150
- the rule.
151
-
152
- .. _match_response :
153
-
154
- match_response
155
- ~~~~~~~~~~~~~~
156
-
157
- .. note ::
158
-
159
- ``match_response `` :ref: `requires the ExpressionLanguage component <requirements >`.
160
-
161
- An ExpressionLanguage expression to decide whether the response should have
162
- the headers applied. If not set, headers are applied if the request is
163
- :term: `safe `. The expression can access the ``Response `` object with the
164
- ``response `` variable. For example, to handle all failed requests, you can do:
165
-
166
- .. code-block :: yaml
167
-
168
- -
169
- match :
170
- match_response : response.getStatusCode() >= 400
171
- # ...
172
-
173
- You can not set both ``match_response `` and ``additional_cacheable_status ``
174
- inside the same rule.
175
-
176
74
headers
177
- -------
75
+ ^^^^^^^
76
+
77
+ **type **: ``array ``
178
78
179
79
.. sidebar :: YAML alias for same headers for different matches
180
80
@@ -208,7 +108,9 @@ In the ``headers`` section, you define what headers to set on the response if
208
108
the request was matched.
209
109
210
110
cache_control
211
- ~~~~~~~~~~~~~
111
+ """""""""""""
112
+
113
+ **type **: ``array ``
212
114
213
115
The map under ``cache_control `` is set in a call to ``Response::setCache() ``.
214
116
The names are specified with underscores in yml, but translated to ``- `` for
@@ -245,7 +147,7 @@ default. If you want it respected, add your own logic to ``vcl_fetch``.
245
147
246
148
The cache-control headers are described in detail in :rfc: `2616#section-14.9 `.
247
149
248
- Extra cache control directives
150
+ Extra Cache Control Directives
249
151
""""""""""""""""""""""""""""""
250
152
251
153
You can also set headers that Symfony considers non-standard, some coming from
@@ -278,7 +180,9 @@ directives are flags that are included when set to true.
278
180
no_transform : true
279
181
280
182
last_modified
281
- ~~~~~~~~~~~~~
183
+ """""""""""""
184
+
185
+ **type **: ``string ``
282
186
283
187
The input to the ``last_modified `` is used for the ``Last-Modified `` header.
284
188
This value must be a valid input to ``DateTime ``.
@@ -300,7 +204,9 @@ This value must be a valid input to ``DateTime``.
300
204
from the cache if it was not invalidated since the client requested it.
301
205
302
206
vary
303
- ~~~~
207
+ """"
208
+
209
+ **type **: ``string ``
304
210
305
211
You can set the `vary ` option to an array that defines the contents of the
306
212
`Vary ` header when matching the request. This adds to existing Vary headers,
@@ -316,8 +222,12 @@ keeping previously set Vary options.
316
222
headers :
317
223
vary : My-Custom-Header
318
224
319
- reverse_proxy_ttl for X-Reverse-Proxy-TTL for Custom Reverse Proxy Time-Outs
320
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225
+ reverse_proxy_ttl
226
+ """""""""""""""""
227
+
228
+ **type **: ``integer ``
229
+
230
+ For X-Reverse-Proxy-TTL for Custom Reverse Proxy Time-Outs
321
231
322
232
By default, reverse proxies use the ``s-maxage `` of your ``Cache-Control `` header
323
233
to know how long it should cache a page. But by default, the s-maxage is also
@@ -367,6 +277,3 @@ you need to extend your varnish ``vcl_fetch`` configuration:
367
277
Note that there is a ``beresp.ttl `` field in VCL but unfortunately it can only
368
278
be set to absolute values and not dynamically. Thus we have to revert to a C
369
279
code fragment.
370
-
371
- .. _Trusting Proxies : http://symfony.com/doc/current/components/http_foundation/trusting_proxies.html
372
- .. _controllers as services : http://symfony.com/doc/current/cookbook/controller/service.html
0 commit comments