Magento多语言商店,带清漆 [英] Magento Multi language store with varnish
问题描述
多语言Magento存储如何与Varnish一起使用. 清漆中有任何可用的配置,因此我们可以基于cookie创建缓存吗?
How Multi-Language Magento store work with Varnish. Is there any configuration available in varnish,so we can create cache base on cookies?
推荐答案
如果您不介意使用不同网址的语言,Turpentine可以为您处理:
If you don't mind the languages being at different urls, Turpentine can handle this for you: https://github.com/nexcess/magento-turpentine/issues/36
如果您希望他们像开箱一样地表现,那就继续吧.
If you want them to behave as they do out of the box, lets keep going.
您必须修改清漆在VCL中生成has的方式 参考: https://www.varnish-cache.org/trac/wiki/VCLExampleCachingLoggedInUsers
You have to modify how varnish generates the has in your VCL Reference: https://www.varnish-cache.org/trac/wiki/VCLExampleCachingLoggedInUsers
我们将对其进行修改,以考虑到Magento基于语言选择器设置的商店Cookie. (遵循以下行为: http://demo.magentocommerce.com )不幸的是,由于Varnish倾向于采用两种方法,所以这变得棘手看到Cookie飞来飞去时,请勿将Cookie传回服务器或不缓存内容
We would modify this to also take into account the store cookie that Magento sets based on the language selector. (Following the behavior here: http://demo.magentocommerce.com) Unfortunately this gets tricky as Varnish tends to either not pass cookies back to the server or not cache things when it sees cookies flying around
这将基于cookie的值以及默认的url和主机拥有Varnish缓存:
This would have Varnish cache based on the value of the cookie as well as the default url and host:
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发送回服务器
But, with this method you might have to tweak the rest of your VCL to cache the page properly AND send the cookies back to the server
另一种选择是使用Cookie来更改任意标头上的缓存,我们将其称为X-Mage-Lang:
Another option is to use the cookie to vary caching on an arbitrary header, lets call it 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
This pattern is also used for device detection with varnish: https://github.com/varnish/varnish-devicedetect/blob/master/INSTALL.rst
然后,您将必须扩展Mage_Core_Model_App以使用此标头而不是"store" cookie.在Magento CE 1.7中,其_checkCookieStore:
Then, you would have to extend Mage_Core_Model_App to use this header instead of the 'store' cookie. In Magento CE 1.7 its _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
You would set the current store on $_SERVER['X-Mage-Lang'] instead of the cookie
这篇关于Magento多语言商店,带清漆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!