Skip to content

Commit 5a4c2fa

Browse files
committed
feat: introduce new reverse_proxy_cache_control config to allow configuring specific cache control for reverse proxy
1 parent b9a80ac commit 5a4c2fa

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

Resources/doc/reference/configuration/headers.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,39 @@ section:
347347
348348
This example adds the header ``X-Reverse-Proxy-TTL: 3600`` to your responses.
349349

350+
``reverse_proxy_cache_control``
351+
"""""""""""""""""""""
352+
353+
**type**: ``array``
354+
355+
The map under ``reverse_proxy_cache_control`` goes with ``ttl_header``.
356+
The names are specified with underscores in yaml, but translated to ``-`` for
357+
the ``X-Reverse-Proxy-TTL`` header.
358+
359+
You can use the standard cache control directives:
360+
361+
* ``max_age`` time in seconds;
362+
* ``s_maxage`` time in seconds for proxy caches (also public caches);
363+
* ``private`` true or false;
364+
* ``public`` true or false;
365+
366+
.. code-block:: yaml
367+
368+
# app/config/config.yml
369+
fos_http_cache:
370+
cache_control:
371+
rules:
372+
-
373+
headers:
374+
reverse_proxy_cache_control:
375+
max_age: 36000
376+
public: true
377+
cache_control:
378+
no_store: true
379+
private: true
380+
381+
This example adds the header ``X-Reverse-Proxy-TTL: max-age=36000, public`` to your responses.
382+
350383
``ttl_header``
351384
--------------
352385

src/DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,15 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
337337
->defaultNull()
338338
->info('Specify a custom time to live in seconds for your caching proxy. This value is sent in the custom header configured in cache_control.ttl_header.')
339339
->end()
340+
->arrayNode('reverse_proxy_cache_control')
341+
->info('Add the specified cache control directives for reverse proxy.')
342+
->children()
343+
->scalarNode('max_age')->end()
344+
->scalarNode('s_maxage')->end()
345+
->booleanNode('private')->end()
346+
->booleanNode('public')->end()
347+
->end()
348+
->end()
340349
->arrayNode('vary')
341350
->beforeNormalization()->ifString()->then(function ($v) {
342351
return preg_split('/\s*,\s*/', $v);

src/EventListener/CacheControlListener.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,30 @@ public function onKernelResponse(ResponseEvent $event): void
116116
}
117117
}
118118

119-
if (array_key_exists('reverse_proxy_ttl', $options)
120-
&& null !== $options['reverse_proxy_ttl']
121-
&& !$response->headers->has($this->ttlHeader)
122-
) {
123-
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
119+
if (!$response->headers->has($this->ttlHeader)) {
120+
if (!empty($options['reverse_proxy_cache_control'])) { // new config
121+
$directives = array_intersect_key($options['reverse_proxy_cache_control'], $this->supportedDirectives);
122+
123+
if (!empty($directives)) {
124+
$headerValues = [];
125+
126+
foreach ($directives as $k => $v) {
127+
$k = str_replace('_', '-', $k);
128+
129+
if (in_array($k, ['public', 'private'])) {
130+
if ($v) {
131+
$headerValues[] = $k;
132+
}
133+
} else {
134+
$headerValues[] = ' ' . $k . '=' . $v;
135+
}
136+
}
137+
138+
$response->headers->set($this->ttlHeader, \trim(\implode(', ', $headerValues)), false);
139+
}
140+
} elseif (array_key_exists('reverse_proxy_ttl', $options) && null !== $options['reverse_proxy_ttl']) { // BC layer
141+
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
142+
}
124143
}
125144

126145
if (!empty($options['vary'])) {

0 commit comments

Comments
 (0)