Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] feat: introduce new reverse_proxy_cache_control config to allow configuring specific cache control for reverse proxy #640

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Resources/doc/reference/configuration/headers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,39 @@ section:

This example adds the header ``X-Reverse-Proxy-TTL: 3600`` to your responses.

``reverse_proxy_cache_control``
"""""""""""""""""""""

**type**: ``array``

The map under ``reverse_proxy_cache_control`` goes with ``ttl_header``.
The names are specified with underscores in yaml, but translated to ``-`` for
the ``X-Reverse-Proxy-TTL`` header.

You can use the standard cache control directives:

* ``max_age`` time in seconds;
* ``s_maxage`` time in seconds for proxy caches (also public caches);
* ``private`` true or false;
* ``public`` true or false;

.. code-block:: yaml

# app/config/config.yml
fos_http_cache:
cache_control:
rules:
-
headers:
reverse_proxy_cache_control:
max_age: 36000
public: true
cache_control:
no_store: true
private: true

This example adds the header ``X-Reverse-Proxy-TTL: max-age=36000, public`` to your responses.

``ttl_header``
--------------

Expand Down
9 changes: 9 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,15 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
->defaultNull()
->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.')
->end()
->arrayNode('reverse_proxy_cache_control')
->info('Add the specified cache control directives for reverse proxy.')
->children()
->scalarNode('max_age')->end()
->scalarNode('s_maxage')->end()
->booleanNode('private')->end()
->booleanNode('public')->end()
->end()
->end()
->arrayNode('vary')
->beforeNormalization()->ifString()->then(function ($v) {
return preg_split('/\s*,\s*/', $v);
Expand Down
29 changes: 24 additions & 5 deletions src/EventListener/CacheControlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,30 @@ public function onKernelResponse(ResponseEvent $event): void
}
}

if (array_key_exists('reverse_proxy_ttl', $options)
&& null !== $options['reverse_proxy_ttl']
&& !$response->headers->has($this->ttlHeader)
) {
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
if (!$response->headers->has($this->ttlHeader)) {
if (!empty($options['reverse_proxy_cache_control'])) {
$directives = array_intersect_key($options['reverse_proxy_cache_control'], $this->supportedDirectives);

if (!empty($directives)) {
$headerValues = [];

foreach ($directives as $k => $v) {
$k = str_replace('_', '-', $k);

if (in_array($k, ['public', 'private'])) {
if ($v) {
$headerValues[] = $k;
}
} else {
$headerValues[] = $k . '=' . $v;
}
}

$response->headers->set($this->ttlHeader, \implode(', ', $headerValues), false);
}
} elseif (array_key_exists('reverse_proxy_ttl', $options) && null !== $options['reverse_proxy_ttl']) {
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
}
}

if (!empty($options['vary'])) {
Expand Down
Loading