简体   繁体   中英

Varnish cache according to custom header

I am using Varnish to serve a website but need to have the caching dependent on Geo location. I have written a small Vmod that uses Ip2Location to set a request header that has the country code in which is then passed to my application (Magento) via Apache which serves up the relevant content but I need Varnish to also cache depending on the country code.

Is there any way to achieve this?

I have sorted this.

In the vcl_hash subroutine I needed to add the country code into the hashed data.

sub vcl_hash {
     hash_data(req.url);
     if (req.http.host) {
         hash_data(req.http.host);
     } else {
         hash_data(server.ip);
     }
     hash_data(req.http.X-Country-Code);

     return (hash);
}

The default vcl I am using had the whole vcl_hash subroutine commented out because it was using req.hash instead of hash_data() which is no longer available in Varnish 3 so adjust as necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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