From 597408f908c3e7b4cdd627e159d95ed5dd4ce215 Mon Sep 17 00:00:00 2001 From: Jeremy Pyne Date: Mon, 6 Jun 2016 15:23:51 -0400 Subject: [PATCH 1/3] Update PurgeCache.php Add code to forward the hostname to varnish when purging the cache. --- .../Magento/CacheInvalidate/Model/PurgeCache.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CacheInvalidate/Model/PurgeCache.php b/app/code/Magento/CacheInvalidate/Model/PurgeCache.php index 22c008097f611..5ec050c6fe0a7 100644 --- a/app/code/Magento/CacheInvalidate/Model/PurgeCache.php +++ b/app/code/Magento/CacheInvalidate/Model/PurgeCache.php @@ -7,10 +7,12 @@ use Magento\Framework\Cache\InvalidateLogger; use Magento\Framework\App\DeploymentConfig; +use Magento\Framework\App\RequestInterface; class PurgeCache { const HEADER_X_MAGENTO_TAGS_PATTERN = 'X-Magento-Tags-Pattern'; + const HEADER_HOST = 'host'; /** * @var \Magento\PageCache\Model\Cache\Server @@ -26,6 +28,11 @@ class PurgeCache * @var InvalidateLogger */ private $logger; + + /** + * @var RequestInterface + */ + private $request; /** * Constructor @@ -37,11 +44,13 @@ class PurgeCache public function __construct( \Magento\PageCache\Model\Cache\Server $cacheServer, \Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory, - InvalidateLogger $logger + InvalidateLogger $logger, + RequestInterface $request ) { $this->cacheServer = $cacheServer; $this->socketAdapterFactory = $socketAdapterFactory; $this->logger = $logger; + $this->request = $request; } /** @@ -55,7 +64,7 @@ public function sendPurgeRequest($tagsPattern) { $socketAdapter = $this->socketAdapterFactory->create(); $servers = $this->cacheServer->getUris(); - $headers = [self::HEADER_X_MAGENTO_TAGS_PATTERN => $tagsPattern]; + $headers = [self::HEADER_X_MAGENTO_TAGS_PATTERN => $tagsPattern, self::HEADER_HOST => $this->request->getHttpHost()]; $socketAdapter->setOptions(['timeout' => 10]); foreach ($servers as $server) { try { From 93e5fc4c2671d1d2ac293f531cd9b8fc88839013 Mon Sep 17 00:00:00 2001 From: Jeremy Pyne Date: Mon, 6 Jun 2016 15:28:14 -0400 Subject: [PATCH 2/3] Update varnish4.vcl Add code to read hostname and sample code to only purge current domain. --- app/code/Magento/PageCache/etc/varnish4.vcl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/code/Magento/PageCache/etc/varnish4.vcl b/app/code/Magento/PageCache/etc/varnish4.vcl index d9931c8eff35a..2e8f8b5681a50 100644 --- a/app/code/Magento/PageCache/etc/varnish4.vcl +++ b/app/code/Magento/PageCache/etc/varnish4.vcl @@ -21,6 +21,8 @@ sub vcl_recv { return (synth(400, "X-Magento-Tags-Pattern header required")); } ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern); + # The preceding will purge all magento site's from the cache, or the following can be used instead fo only clear the current domain. + # ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern + " && obj.http.X-Req-Host == " + req.http.host); return (synth(200, "Purged")); } @@ -107,6 +109,8 @@ sub vcl_backend_response { if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") { set beresp.do_gzip = true; } + + set beresp.http.X-Req-Host = bereq.http.host; # cache only successfully responses and 404s if (beresp.status != 200 && beresp.status != 404) { @@ -154,4 +158,5 @@ sub vcl_deliver { unset resp.http.X-Varnish; unset resp.http.Via; unset resp.http.Link; + unset resp.http.X-Req-Host; } From 027d61dd124471cd3956f8f62bf6f408e48355cb Mon Sep 17 00:00:00 2001 From: Jeremy Pyne Date: Mon, 6 Jun 2016 15:29:32 -0400 Subject: [PATCH 3/3] Update varnish3.vcl Added code to track hostname and sample code to purge only the current domain. --- app/code/Magento/PageCache/etc/varnish3.vcl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/code/Magento/PageCache/etc/varnish3.vcl b/app/code/Magento/PageCache/etc/varnish3.vcl index 70a56aa46f052..0397f01f3a507 100644 --- a/app/code/Magento/PageCache/etc/varnish3.vcl +++ b/app/code/Magento/PageCache/etc/varnish3.vcl @@ -28,6 +28,8 @@ sub vcl_recv { error 400 "X-Magento-Tags-Pattern header required"; } ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern); + # The preceding will purge all magento site's from the cache, or the following can be used instead fo only clear the current domain. + # ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern + " && obj.http.X-Req-Host == " + req.http.host); error 200 "Purged"; } @@ -84,6 +86,8 @@ sub vcl_fetch { if (req.url ~ "\.js$" || beresp.http.content-type ~ "text") { set beresp.do_gzip = true; } + + set beresp.http.X-Req-Host = bereq.http.host; # cache only successfully responses and 404s if (beresp.status != 200 && beresp.status != 404) { @@ -128,4 +132,5 @@ sub vcl_deliver { unset resp.http.X-Varnish; unset resp.http.Via; unset resp.http.Link; + unset resp.http.X-Req-Host; }