Skip to content

Commit a9e3d20

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

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

Resources/doc/reference/configuration/headers.rst

+33
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 ``Cache-Control`` 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/EventListener/CacheControlListener.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,18 @@ 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'])) {
121+
$directives = array_intersect_key($options['reverse_proxy_cache_control'], $this->supportedDirectives);
122+
if (!empty($directives)) {
123+
$directives = array_map(function ($key) {return str_replace('_', '-', $key);}, $directives);
124+
$response->headers->set($this->ttlHeader, implode(' ', $directives), false);
125+
}
126+
} elseif (array_key_exists('reverse_proxy_ttl', $options)
127+
&& null !== $options['reverse_proxy_ttl']
128+
) {
129+
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
130+
}
124131
}
125132

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

0 commit comments

Comments
 (0)