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

Code to pass hostname to Varnish and optimally only purge the current domain. #4905

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 11 additions & 2 deletions app/code/Magento/CacheInvalidate/Model/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +28,11 @@ class PurgeCache
* @var InvalidateLogger
*/
private $logger;

/**
* @var RequestInterface
*/
private $request;

/**
* Constructor
Expand All @@ -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;
}

/**
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/PageCache/etc/varnish3.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions app/code/Magento/PageCache/etc/varnish4.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}