2016-11-11 52 views

回答

0

您可以设置一个包含相关url的标头来缓存并更改varnish散列对象的方式。

sub vcl_recv { 
    ... 
    # Remove the params you don't want from the query string and set it to a 
    # temp header (Varnish does not have variables) 
    set req.http.x-cache-url = regsub(req.url,"(utm1=.*&|utm1=.*)", "") 
    ... 
} 

# Below is the default vcl_hash but swapping the req.url for req.http.x-cache-url 
sub vcl_hash { 
    hash_data(req.http.x-cache-url); 
    if (req.http.host) { 
    hash_data(req.http.host); 
    } else { 
    hash_data(server.ip); 
    } 
    return (lookup); 
} 

这样做,你将能够保持您的查询字符串,因为它是和你仍然能够提供同样的缓存不同PARAMS。

注意regsub是你操纵params的地方。随意根据您的需求进行更改。

0

更好的解决方案是重写传入的URL,而不是引入不必要的VCL。这也将照顾所有的谷歌相关的JavaScript变量,而不仅仅是utm_ones:

if (req.url ~ "(\?|&)(gclid|utm_[a-z]+)=") { 
    set req.url = regsuball(req.url, "(gclid|utm_[a-z]+)=[-_A-z0-9\+\(\)%]+&?", ""); 
    set req.url = regsub(req.url, "(\?|&)$", ""); 
} 

查看更多在原来的职位上stripping Google Analytics variables with Varnish