我在PHP中有这样的代码
//get referer
$reffere_Url = $_SERVER['HTTP_REFERER'];
//if referer is abc.com then show the referer slider at home.
if (strpos($reffere_Url, 'abc') !== false && $home_referal_slider!="")
{
$home_slider = $home_referal_slider;
}
else
{
$home_slider = $home_slider;
}
但即使条件成立,它总是返回$ home_slider。 这是因为安装了清漆及其服务缓存版本的网页。请帮助如何实现同样的目标。
答案 0 :(得分:0)
我试图在vcl_rec中检查req.http.Referer值以返回(传递)但它没有用。
还有另一个解决方案,只需将一个查询字符串(?ex = exTrDz)添加到referer页面上的url。这样就可以了。
答案 1 :(得分:0)
You need to tell Varnish what and how to cache.
First option is to override the vcl_hash
in Varnish to include the referer:
sub vcl_hash {
hash_data(req.http.referer);
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
But this will probably impact you cache ratio very badly - every different referer will get their unique cache pool.
Much neater way is to use Vary
headers from the backend - this again lets Varnish know that this response cache depends on the referer header. I use mostly Vary
headers so I can control from the backend how Varnish behaves.
In vanilla PHP:
header('Vary', 'Referer');
But note that this needs to be set regardless of which $home_slider
you serve. So this again will impact your cache ratio badly if you have the logic on every page.
One more option would be to use Edge Side Includes (ESI) to just get the slider from the backend so that the rest of the page can be cached normally, but this is much more complicated.
So probably the best solution would be to pass all the traffic with referer abc.com to the backend. Hoping this is not the majority of your visitors it has the least impact on your cache ratio.
sub vcl_recv {
if (req.http.referer ~ "abc\.com") {
return (pass);
}
}
(Examples based on Varnish 4)