varnish vcl用于剥离查询参数但不是全部

时间:2014-08-05 17:37:03

标签: caching varnish varnish-vcl

在varnish vcl逻辑中,有没有办法去除URL中的所有查询参数,除了影响页面行为的少数几个。

例如,

 www.xyz.com/search?city=sandiego&state=CA&type=xml&number=321&key=afdsf34345

想要保留“城市”和“类型”,但其他任何被剥夺的东西。

我尝试使用regsub,但它没有用。

赞赏任何指针

1 个答案:

答案 0 :(得分:0)

以下似乎有效:

$ cat test.vtc 
varnishtest "Remove certain parameters."

server s1 {
  rxreq
  txresp -body "made it!"
} -start

varnish v1 -vcl {
  backend default {
    .host = "${s1_addr}";
    .port = "${s1_port}";
  }

  sub vcl_recv {
      if (req.url ~ "\?") {
          set req.http.X_City = regsub(req.url,  ".*[?&](city)=(\w+).*", "\1=\2");
          set req.http.X_Type = regsub(req.url,  ".*[?&](type)=(\w+).*", "\1=\2");
          set req.url = regsub(req.url,  "(.*)\?.*", "\1");
          set req.url = req.url + "?" + req.http.X_City + "&" + req.http.X_Type;
          unset req.http.X_City;
          unset req.http.X_Type;
      }
  }

  sub vcl_deliver {
      set resp.http.X_Test_Result = req.url;
  }
} -start

client c1 {
  txreq -url "/search?city=sandiego&state=CA&type=xml&number=321&key=afdsf34345"
  rxresp

  expect resp.http.X_Test_Result == "/search?city=sandiego&type=xml"
  expect resp.status == 200
  expect resp.body == "made it!"

} -run

varnish v1 -expect client_req == 1

通过varnishtest次传递:

$ varnishtest test.vtc 
#     top  TEST test.vtc passed (1.341)