多语言Magento如何与Varnish一起使用。清漆中是否有可用的配置,因此我们可以在Cookie上创建缓存?
如果你不介意语言在不同的网址,Turpentine可以为你处理:https://github.com/nexcess/magento-turpentine/issues/36
如果你希望他们表现得像开箱即用,那就继续吧。
您必须修改varnish在VCL参考中生成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=(.*?))(?:;|$)");
}
此模式还用于使用清漆进行设备检测: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上设置当前商店
在Varnish Config中添加以下行,
if(beresp.http.Set-Cookie) {
return (hit_for_pass);
}