Search code examples
ubuntunginxvarnishvarnish-vcl

How to Log varnish cache key?


sub vcl_hash {
    hash_data(req.http.Host + req.url);
}

This is my varnish vcl_hash . Can someone enlighten me with configuration to log the cache key. so that I can verify what's the key being set.

I'm using varnish -6.6.1 version.


Solution

  • The built-in VCL behavior combines the URL and the Host header to compose the cache key. The implementation is slightly different from your example.

    If you use a tool like varnishlog to inspect the Varnish logs, the cache key is not included in the output by default. You can unlock this using the vsl_mask runtime parameter.

    If you're only planning to temporarily visualize the hash, you can use the following command to enable it without persisting the setting:

    varnishadm param.set vsl_mask +Hash
    

    If you want to ensure the hash is always part of the VSL output, please edit your varnishd runtime configuration and add -p vsl_mask=+Hash.

    Once the VSL mask is set, you can run the following command to test this:

    varnishlog -g request -i requrl -I reqHeader:Host -i Hash
    

    This command will return the logs for all transactions, but will only include the ReqURL tag, the ReqHeader tag for the Host header and the corresponding Hash tags.

    Here's an example of what that could like like for http://localhost/:

    *   << Request  >> 10
    -   ReqURL         /
    -   ReqHeader      Host: localhost
    -   Hash           "/%00"
    -   Hash           "localhost%00"