問題描述
使用 Varnish 更改標頭中的引用者 (Change Referrer in header using Varnish)
I think this is a possiblity with varnish where you can change the referrer in the header of its users and then serve them the content either from cache or from the server. I want to know how can that be made possible.
I tried this with "req.http.referer" and then "set req.http.referer" in varnish 2.1 on centos 32‑bit machine but it didn't work when i checked the results with the command "varnishtop ‑i TxHeader ‑I Referer".
Anyone got any other ideas better than this?
‑‑‑‑‑
參考解法
方法 1:
At least on Varnish 3.0 the following works as expected. Obviously if the response is served from cache and you are not using the req.http.Referer for hash(), it doesn't matter how you change the referer header.
# Modify Referer header
sub vcl_recv {
if (req.http.Referer) {
# Referer was set. Replace foo with bar
set req.http.Referer = regsub(req.http.Referer,"foo","bar");
} else {
# Referer was not set. Set it to something anyway.
set req.http.Referer = "http://referer.was.empty/";
}
}
Also note that varnishtop ‑i TxHeader ‑I Referer
is case sensitive. If you set req.http.referer
then it will not match ‑I Referer
even though your HTTP backend will understand the referer:
header as well (according to RFC 2612 4.2 message headers are case insensitive).