简体   繁体   English

如何在Varnish 4中覆盖vcl_hash方法

[英]How to Override vcl_hash Method in Varnish 4

I have the following code snip-it for Varnish 2 to only include a subset of fields in it's hashing logic. 我有以下代码片段 - 它为Varnish 2只包含其散列逻辑中的字段子集。

sub vcl_hash {
  set req.http.temp = regsub(req.url, "^.*[?&](q=[^&]*).*$", "\1");
  set req.http.temp2 = regsub(req.url, "^.*[?&](limit=[^&]*).*$", "\1");
  set req.http.temp3 = regsub(req.url, "^.*[?&](classes=[^&]*).*$", "\1");
  set req.http.temp3 = regsub(req.url, "^.*[?&](offset=[^&]*).*$", "\1");
  set req.hash += req.http.temp;
  set req.hash += req.http.temp2;
  set req.hash += req.http.temp3;
  set req.hash += req.http.temp4;
  return(hash); 
}

How can I convert this code to work with Varnish 4? 如何将此代码转换为与Varnish 4一起使用?

You can use the built-in function hash_data 您可以使用内置函数hash_data

First, call the hash_data function with your url but without the query string. 首先,使用您的url调用hash_data函数,但不使用查询字符串。 Then call again the hash_data function for every fields you want to add to the hash logic. 然后再次为要添加到哈希逻辑的每个字段调用hash_data函数。

You should end up with something like that : 你应该得到类似的东西:

sub vlc_hash {
  hash_data(__YOUR_REQUEST_WITHOUT_QUERY_STRING__);
  hash_data(regsub(req.url, "^.*[?&](q=[^&]*).*$", "\1"));
  hash_data(regsub(req.url, "^.*[?&](limit=[^&]*).*$", "\1"));
  hash_data(regsub(req.url, "^.*[?&](classes=[^&]*).*$", "\1"));
  hash_data(regsub(req.url, "^.*[?&](offset=[^&]*).*$", "\1"));
  return (lookup);
}

Note that your vlc_hash function must return lookup to abort the execution of the default VCL. 请注意,您的vlc_hash函数必须返回lookup以中止默认VCL的执行。

You can read the documentation of the Varnish 4 hashing logic at the following address https://www.varnish-cache.org/docs/4.0/users-guide/vcl-hashing.html 您可以在以下地址阅读Varnish 4散列逻辑的文档https://www.varnish-cache.org/docs/4.0/users-guide/vcl-hashing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM