run*_*mok 12 regex varnish query-string varnish-vcl
我的目标是将某些查询字符串属性及其值"白名单",以便清除不会改变网址之间的缓存.
例:
Url 1: http://foo.com/someproduct.html?utm_code=google&type=hello  
Url 2: http://foo.com/someproduct.html?utm_code=yahoo&type=hello  
Url 3: http://foo.com/someproduct.html?utm_code=yahoo&type=goodbye
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我想将"utm_code"列入白名单,但不要"输入".因此,在第一个网址被点击后,我希望varnish将缓存的内容提供给第二个网址.
但是,在第三个url的情况下,属性"type"值不同,因此应该是清漆缓存未命中.
我已经尝试了下面的两种方法(在我现在找不到的drupal帮助文章中找到)似乎不起作用.可能是因为我有正则表达式错误.
# 1. strip out certain querystring values so varnish does not vary cache.
set req.url = regsuball(req.url, "([\?|&])utm_(campaign|content|medium|source|term)=[^&\s]*&?", "\1");
# get rid of trailing & or ?
set req.url = regsuball(req.url, "[\?|&]+$", "");
# 2. strip out certain querystring values so varnish does not vary cache.
set req.url = regsuball(req.url, "([\?|&])utm_campaign=[^&\s]*&?", "\1");
set req.url = regsuball(req.url, "([\?|&])foo_bar=[^&\s]*&?", "\1");
set req.url = regsuball(req.url, "([\?|&])bar_baz=[^&\s]*&?", "\1");
# get rid of trailing & or ?
set req.url = regsuball(req.url, "[\?|&]+$", "");
Run Code Online (Sandbox Code Playgroud)
    run*_*mok 12
我想出来并希望分享.我发现这个代码创建了一个能够满足我需要的子程序.
sub vcl_recv {
    # strip out certain querystring params that varnish should not vary cache by
    call normalize_req_url;
    # snip a bunch of other code
}
sub normalize_req_url {
    # Strip out Google Analytics campaign variables. They are only needed
    # by the javascript running on the page
    # utm_source, utm_medium, utm_campaign, gclid, ...
    if(req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=") {
        set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=[%.-_A-z0-9]+&?", "");
    }
    set req.url = regsub(req.url, "(\?&?)$", "");
}
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           16277 次  |  
        
|   最近记录:  |