我的目标是将某些查询字符串属性及其值“白名单”,这样清漆不会改变网址之间的缓存。
示例:
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
在上面的示例中,我想将“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, "[\?|&]+$", "");
答案 0 :(得分: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, "(\?&?)$", "");
}
答案 1 :(得分:3)
RegEx有问题。
我更改了两个regsub调用中使用的RegExes:
sub normalize_req_url {
# Clean up root URL
if (req.url ~ "^/(?:\?.*)?$") {
set 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, "(\?&|\?|&)$", "");
}
第一个更改是“[%。_ A-z0-9-]”部分,因为短划线的功能类似于范围符号,这就是为什么我将它移动到最后,并且应该转义点。
第二个更改不仅是删除剩余网址上的问号,还会删除&符号或问号和&符号。
答案 2 :(得分:1)
来自https://github.com/mattiasgeniar/varnish-4.0-configuration-templates:
# Some generic URL manipulation, useful for all templates that follow
# First remove the Google Analytics added parameters, useless for our backend
if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=") {
set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "");
set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?");
set req.url = regsub(req.url, "\?&", "?");
set req.url = regsub(req.url, "\?$", "");
}
答案 3 :(得分:1)
我通过添加对空params的支持并对剩余的params进行排序来改进runamok的回答,这里是我为验证正确性而实现的完整vtc文件。
varnishtest "Test for URL normalization - Varnish 4"
server s1 {
rxreq
txresp -hdr "Backend: up" -body "Some content"
} -repeat 11 -start
varnish v1 -vcl+backend {
import std;
sub vcl_recv {
# Strip out marketing variables. They are only needed by
# the javascript running on the page.
if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)(=|&|$)") {
# Process params with value.
set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=[%.\-_A-z0-9]+&?", "");
# Process params without value.
set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=?(&|$)", "");
}
# Remove trailing '?', '?&'
set req.url = regsub(req.url, "(\?&?)$", "");
# Sort query params, also removes trailing '&'
set req.url = std.querysort(req.url);
}
sub vcl_deliver {
set resp.http.X-Normalized-URL = req.url;
}
} -start
client c1 {
# Basic, no params.
txreq -url "/test/some-url"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# One blacklisted param.
txreq -url "/test/some-url?utm_campaign=1"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# One blacklisted param, without value.
txreq -url "/test/some-url?utm_campaign"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# Two blacklisted params.
txreq -url "/test/some-url?utm_campaign=1&origin=hpg"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# Two blacklisted params, one without value
txreq -url "/test/some-url?utm_campaign&origin=123-abc%20"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# Two blacklisted params, both without value
txreq -url "/test/some-url?utm_campaign&origin="
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# Three blacklisted params.
txreq -url "/test/some-url?utm_campaign=ABC&origin=hpg&siteurl=br2"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# Three blacklisted params, two without value
txreq -url "/test/some-url?utm_campaign=1&origin=&siteurl"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url"
# Three blacklisted params; one param to keep, with space encoded as +.
txreq -url "/test/some-url?qss=hello+one&utm_campaign=some-value&origin=hpg&siteurl=br2"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url?qss=hello+one"
# Three blacklisted params; one param to keep, with space encoded as %20, passed in-between blacklisted ones.
txreq -url "/test/some-url?utm_campaign=1&qss=hello%20one&origin=hpg&siteurl=br2"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url?qss=hello%20one"
# Three blacklisted params; three params to keep.
txreq -url "/test/some-url?utm_campaign=a-value&qss=hello+one&origin=hpg&siteurl=br2&keep2=abc&keep1"
rxresp
expect resp.http.X-Normalized-URL == "/test/some-url?keep1&keep2=abc&qss=hello+one"
} -run
varnish v1 -expect client_req == 11
答案 4 :(得分:0)
您想要删除utm_code
,但它不在您正在使用的任何正则表达式中。
试试这个:
# Strip out specific utm_ values from request URL query parameters
set req.url = regsuball(req.url, "([\?|&])utm_(campaign|content|medium|source|term|code)=[^&\s]*&?", "\1");
# get rid of trailing & or ?
set req.url = regsuball(req.url, "[\?|&]+$", "");
或者,如果您要删除以utm_
开头的所有网址参数,您可以选择:
# Strip out ALL utm_ values from request URL query parameters
set req.url = regsuball(req.url, "([\?|&])utm_(\w+)=[^&\s]*&?", "\1");
# get rid of trailing & or ?
set req.url = regsuball(req.url, "[\?|&]+$", "");
答案 5 :(得分:0)
runamok的副本,但我在我的参数中得到了+而不是%20,所以我将其添加到我的正则表达式
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.
# I allso stribe facebook local that are use for facebook javascript.
# They are only neededby 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]+|fb_local|mr:[A-z]+)=") {
set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|fb_local|mr:[A-z]+)=[%.+-_A-z0-9]+&?", "");
}
set req.url = regsub(req.url, "(\?&?)$", "");
}
答案 6 :(得分:0)
你们有人试试这个吗? https://github.com/Dridi/libvmod-querystring
实施例
set req.url = querystring.regfilter(req.url," utm _。*");