From 99dc6ac1af66fd88cc741f93464df6a2754243df Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 20 Feb 2024 09:47:44 +0200 Subject: [PATCH 1/6] UHF-9620: Add assets to Varnish cache, new asset purge configuration --- docker-compose.yml | 3 +- public/sites/default/settings.php | 91 +++++++++++++++++-------------- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index a543bc2..df1ffe0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,7 +76,8 @@ services: container_name: "${COMPOSE_PROJECT_NAME}-varnish" image: druidfi/varnish:6-drupal environment: - VARNISH_BACKEND_HOST: "${COMPOSE_PROJECT_NAME}-app" + VARNISH_BACKEND_HOST: "app" + VARNISH_CACHE_STATIC_FILES: "1" depends_on: - app networks: diff --git a/public/sites/default/settings.php b/public/sites/default/settings.php index f33385a..2d67ded 100644 --- a/public/sites/default/settings.php +++ b/public/sites/default/settings.php @@ -151,20 +151,6 @@ function drupal_get_env(string|array $variables) : mixed { } -if ($varnish_host = getenv('DRUPAL_VARNISH_HOST')) { - $config['varnish_purger.settings.default']['hostname'] = $varnish_host; - $config['varnish_purger.settings.varnish_purge_all']['hostname'] = $varnish_host; - - if (!isset($config['system.performance']['cache']['page']['max_age'])) { - $config['system.performance']['cache']['page']['max_age'] = 86400; - } -} - -if ($varnish_port = getenv('DRUPAL_VARNISH_PORT')) { - $config['varnish_purger.settings.default']['port'] = $varnish_port; - $config['varnish_purger.settings.varnish_purge_all']['port'] = $varnish_port; -} - if ($navigation_authentication_key = getenv('DRUPAL_NAVIGATION_API_KEY')) { $config['helfi_navigation.api']['key'] = $navigation_authentication_key; } @@ -182,34 +168,59 @@ function drupal_get_env(string|array $variables) : mixed { $config['helfi_api_base.environment_resolver.settings']['environment_name'] = getenv('APP_ENV'); $config['helfi_api_base.environment_resolver.settings']['project_name'] = getenv('PROJECT_NAME'); -// settings.php doesn't know about existing configuration yet so we can't -// just append new headers to an already existing headers array here. -// If you have configured any extra headers in your purge settings -// you must add them in your all.settings.php as well. -// @todo Replace this with config override service? -$config['varnish_purger.settings.default']['headers'] = [ - [ - 'field' => 'Cache-Tags', - 'value' => '[invalidation:expression]', - ], -]; +if ($varnish_host = getenv('DRUPAL_VARNISH_HOST')) { + // settings.php doesn't know about existing configuration yet so we can't + // just append new headers to an already existing headers array here. + // If you have configured any extra headers in your purge settings + // you must add them in your all.settings.php as well. + // @todo Replace this with config override service? + $varnishConfiguration = [ + 'default' => [ + [ + 'field' => 'Cache-Tags', + 'value' => '[invalidation:expression]', + ], + ], + 'assets' => [ + [ + 'field' => 'X-VC-Purge-Method', + 'value' => 'regex', + ], + [ + 'field' => 'Host', + 'value' => 'varnish-helfi-kymp.docker.so', + ], + ], + 'varnish_purge_all' => [ + [ + 'field' => 'X-VC-Purge-Method', + 'value' => 'regex', + ], + ], + ]; -$config['varnish_purger.settings.varnish_purge_all']['headers'] = [ - [ - 'field' => 'X-VC-Purge-Method', - 'value' => 'regex', - ], -]; + foreach ($varnishConfiguration as $name => $headers) { + $config['varnish_purger.settings.' . $name]['hostname'] = $varnish_host; -if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) { - $config['varnish_purger.settings.default']['headers'][] = [ - 'field' => 'X-VC-Purge-Key', - 'value' => $varnish_purge_key, - ]; - $config['varnish_purger.settings.varnish_purge_all']['headers'][] = [ - 'field' => 'X-VC-Purge-Key', - 'value' => $varnish_purge_key, - ]; + if ($varnish_port = getenv('DRUPAL_VARNISH_PORT')) { + $config['varnish_purger.settings.' . $name]['port'] = $varnish_port; + } + + if (!isset($config['system.performance']['cache']['page']['max_age'])) { + $config['system.performance']['cache']['page']['max_age'] = 86400; + } + } + + foreach ($headers as $header) { + $config['varnish_purger.settings.' . $name]['headers'][] = $header; + } + + if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) { + $config['varnish_purger.settings.' . $name]['headers'][] = [ + 'field' => 'X-VC-Purge-Key', + 'value' => $varnish_purge_key, + ]; + } } if ($stage_file_proxy_origin = getenv('STAGE_FILE_PROXY_ORIGIN')) { From 9ccc4f0bad2c551810e01097ff18d0160f2939fb Mon Sep 17 00:00:00 2001 From: tuutti Date: Tue, 20 Feb 2024 10:07:43 +0200 Subject: [PATCH 2/6] UHF-9620: Fix varnish host --- public/sites/default/settings.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/public/sites/default/settings.php b/public/sites/default/settings.php index 2d67ded..9d74142 100644 --- a/public/sites/default/settings.php +++ b/public/sites/default/settings.php @@ -169,6 +169,17 @@ function drupal_get_env(string|array $variables) : mixed { $config['helfi_api_base.environment_resolver.settings']['project_name'] = getenv('PROJECT_NAME'); if ($varnish_host = getenv('DRUPAL_VARNISH_HOST')) { + if (!isset($config['system.performance']['cache']['page']['max_age'])) { + $config['system.performance']['cache']['page']['max_age'] = 86400; + } + $varnish_backend = parse_url($drush_options_uri, PHP_URL_HOST); + + if (getenv('APP_ENV') === 'local') { + // Varnish backend is something like varnish-helfi-kymp.docker.so on + // local env. + $varnish_backend = 'varnish-' . $varnish_backend; + } + // settings.php doesn't know about existing configuration yet so we can't // just append new headers to an already existing headers array here. // If you have configured any extra headers in your purge settings @@ -188,7 +199,7 @@ function drupal_get_env(string|array $variables) : mixed { ], [ 'field' => 'Host', - 'value' => 'varnish-helfi-kymp.docker.so', + 'value' => $varnish_backend, ], ], 'varnish_purge_all' => [ @@ -205,10 +216,6 @@ function drupal_get_env(string|array $variables) : mixed { if ($varnish_port = getenv('DRUPAL_VARNISH_PORT')) { $config['varnish_purger.settings.' . $name]['port'] = $varnish_port; } - - if (!isset($config['system.performance']['cache']['page']['max_age'])) { - $config['system.performance']['cache']['page']['max_age'] = 86400; - } } foreach ($headers as $header) { From 5274e468af921367a8c02789addb776b4d695811 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 22 Feb 2024 09:07:16 +0200 Subject: [PATCH 3/6] UHF-9620: Longer cache TTL --- public/sites/default/settings.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/sites/default/settings.php b/public/sites/default/settings.php index 9d74142..6683510 100644 --- a/public/sites/default/settings.php +++ b/public/sites/default/settings.php @@ -169,9 +169,9 @@ function drupal_get_env(string|array $variables) : mixed { $config['helfi_api_base.environment_resolver.settings']['project_name'] = getenv('PROJECT_NAME'); if ($varnish_host = getenv('DRUPAL_VARNISH_HOST')) { - if (!isset($config['system.performance']['cache']['page']['max_age'])) { - $config['system.performance']['cache']['page']['max_age'] = 86400; - } + // Cache everything for 1 year by default. + $config['system.performance']['cache']['page']['max_age'] = 31536000; + $varnish_backend = parse_url($drush_options_uri, PHP_URL_HOST); if (getenv('APP_ENV') === 'local') { From bca5fb1b2c1e95821ded9338dec6b2bb8e18ffd6 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 22 Feb 2024 10:39:49 +0200 Subject: [PATCH 4/6] Update docker-compose.yml --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index df1ffe0..14960c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,7 +76,7 @@ services: container_name: "${COMPOSE_PROJECT_NAME}-varnish" image: druidfi/varnish:6-drupal environment: - VARNISH_BACKEND_HOST: "app" + VARNISH_BACKEND_HOST: "${COMPOSE_PROJECT_NAME}-app" VARNISH_CACHE_STATIC_FILES: "1" depends_on: - app From fcbaa9e44206cca482535659e84a33b068ebf725 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 22 Feb 2024 12:35:36 +0200 Subject: [PATCH 5/6] UHF-9620: Fixed loop --- public/sites/default/settings.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/sites/default/settings.php b/public/sites/default/settings.php index 6683510..fadca81 100644 --- a/public/sites/default/settings.php +++ b/public/sites/default/settings.php @@ -216,10 +216,10 @@ function drupal_get_env(string|array $variables) : mixed { if ($varnish_port = getenv('DRUPAL_VARNISH_PORT')) { $config['varnish_purger.settings.' . $name]['port'] = $varnish_port; } - } - foreach ($headers as $header) { - $config['varnish_purger.settings.' . $name]['headers'][] = $header; + foreach ($headers as $header) { + $config['varnish_purger.settings.' . $name]['headers'][] = $header; + } } if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) { From a779f4f8b0f03740369b15a69342937c5b85b4bf Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 22 Feb 2024 12:58:27 +0200 Subject: [PATCH 6/6] UHF-9620: Fixed loop (again) --- public/sites/default/settings.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/public/sites/default/settings.php b/public/sites/default/settings.php index fadca81..9006eef 100644 --- a/public/sites/default/settings.php +++ b/public/sites/default/settings.php @@ -220,13 +220,13 @@ function drupal_get_env(string|array $variables) : mixed { foreach ($headers as $header) { $config['varnish_purger.settings.' . $name]['headers'][] = $header; } - } - if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) { - $config['varnish_purger.settings.' . $name]['headers'][] = [ - 'field' => 'X-VC-Purge-Key', - 'value' => $varnish_purge_key, - ]; + if ($varnish_purge_key = getenv('VARNISH_PURGE_KEY')) { + $config['varnish_purger.settings.' . $name]['headers'][] = [ + 'field' => 'X-VC-Purge-Key', + 'value' => $varnish_purge_key, + ]; + } } }