如果您不介意语言位于不同的 url,Turpentine 可以为您处理此问题:https://github.com/nexcess/magento-turpentine/issues/36 https://github.com/nexcess/magento-turpentine/issues/36
如果您希望它们像开箱即用的那样表现,那就继续吧。
您必须修改 varnish 在 VCL 中生成 has 的方式
参考:https://www.varnish-cache.org/trac/wiki/VCLExampleCachingLoggedInUsers https://www.varnish-cache.org/trac/wiki/VCLExampleCachingLoggedInUsers
我们将对其进行修改,以考虑 Magento 基于语言选择器设置的存储 cookie。 (遵循此处的行为:http://demo.magentocommerce.com http://demo.magentocommerce.com)不幸的是,这会变得很棘手,因为 Varnish 往往要么不将 cookie 传回服务器,要么在看到 cookie 到处乱飞时不缓存内容
这将具有基于 cookie 值以及默认 url 和主机的 Varnish 缓存:
sub vcl_hash {
hash_data(req.url);
hash_data(req.http.host);
if (req.http.Cookie ~ "(?:^|;\s*)(?:store=(.*?))(?:;|$)"){
hash_data(regsub(req.http.Cookie, "(?:^|;\s*)(?:store=(.*?))(?:;|$)"));
}
return (hash);
}
但是,使用这种方法,您可能需要调整 VCL 的其余部分以正确缓存页面并将 cookie 发送回服务器
另一种选择是使用 cookie 来改变任意标头上的缓存,我们将其称为 X-Mage-Lang:
sub vcl_fetch {
#can do this better with regex
if (req.http.Cookie ~ "(?:^|;\s*)(?:store=(.*?))(?:;|$)"){
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-Mage-Lang";
} elseif (beresp.http.Vary !~ "X-Mage-Lang") { # add to existing Vary
set beresp.http.Vary = beresp.http.Vary + ", X-Mage-Lang";
}
}
# comment this out if you don't want the client to know your classification
set beresp.http.X-Mage-Lang = regsub(req.http.Cookie, "(?:^|;\s*)(?:store=(.*?))(?:;|$)");
}
此模式也用于使用清漆进行设备检测:https://github.com/varnish/varnish-devicedetect/blob/master/INSTALL.rst https://github.com/varnish/varnish-devicedetect/blob/master/INSTALL.rst
然后,您必须扩展 Mage_Core_Model_App 才能使用此标头而不是“store”cookie。在 Magento CE 1.7 中,其 _checkCookieStore :
protected function _checkCookieStore($type)
{
if (!$this->getCookie()->get()) {
return $this;
}
$store = $this->getCookie()->get(Mage_Core_Model_Store::COOKIE_NAME);
if ($store && isset($this->_stores[$store])
&& $this->_stores[$store]->getId()
&& $this->_stores[$store]->getIsActive()) {
if ($type == 'website'
&& $this->_stores[$store]->getWebsiteId() == $this->_stores[$this->_currentStore]->getWebsiteId()) {
$this->_currentStore = $store;
}
if ($type == 'group'
&& $this->_stores[$store]->getGroupId() == $this->_stores[$this->_currentStore]->getGroupId()) {
$this->_currentStore = $store;
}
if ($type == 'store') {
$this->_currentStore = $store;
}
}
return $this;
}
您可以在 $_SERVER['X-Mage-Lang'] 而不是 cookie 上设置当前存储