多语言 Magento 商店如何使用 Varnish。varnish 中是否有可用的配置,所以我们可以基于 cookie 创建缓存?
2 回答
如果您不介意使用不同 url 的语言,Turpentine 可以为您处理:https ://github.com/nexcess/magento-turpentine/issues/36
如果您希望他们表现得像开箱即用一样,让我们继续。
您必须在 VCL 参考中修改 varnish 生成 has 的方式:https ://www.varnish-cache.org/trac/wiki/VCLExampleCachingLoggedInUsers
我们会修改它以考虑 Magento 基于语言选择器设置的商店 cookie。(遵循此处的行为: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=(.*?))(?:;|$)");
}
此模式也用于使用 varnish 进行设备检测:https ://github.com/varnish/varnish-devicedetect/blob/master/INSTALL.rst
然后,您必须扩展 Mage_Core_Model_App 以使用此标头而不是“存储”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 上设置当前存储
在 Varnish Config 中添加以下行,
if(beresp.http.Set-Cookie) {
return (hit_for_pass);
}