File manager - Edit - /usr/share/doc/varnish/html/users-guide/vcl-hashing.html
Back
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Hashing — Varnish version 7.1.1 documentation</title> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="../_static/classic.css" type="text/css" /> <script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="Grace mode and keep" href="vcl-grace.html" /> <link rel="prev" title="Backend servers" href="vcl-backends.html" /> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="vcl-grace.html" title="Grace mode and keep" accesskey="N">next</a> |</li> <li class="right" > <a href="vcl-backends.html" title="Backend servers" accesskey="P">previous</a> |</li> <li class="nav-item nav-item-0"><a href="../index.html">Varnish version 7.1.1 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="index.html" >The Varnish Users Guide</a> »</li> <li class="nav-item nav-item-2"><a href="vcl.html" accesskey="U">VCL - Varnish Configuration Language</a> »</li> <li class="nav-item nav-item-this"><a href="">Hashing</a></li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="hashing"> <h1>Hashing<a class="headerlink" href="#hashing" title="Permalink to this headline">¶</a></h1> <p>Internally, when Varnish stores content in the cache indexed by a hash key used to find the object again. In the default setup this key is calculated based on <cite>URL</cite>, the <cite>Host:</cite> header, or if there is none, the IP address of the server:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sub</span> <span class="n">vcl_hash</span> <span class="p">{</span> <span class="n">hash_data</span><span class="p">(</span><span class="n">req</span><span class="o">.</span><span class="n">url</span><span class="p">);</span> <span class="k">if</span> <span class="p">(</span><span class="n">req</span><span class="o">.</span><span class="n">http</span><span class="o">.</span><span class="n">host</span><span class="p">)</span> <span class="p">{</span> <span class="n">hash_data</span><span class="p">(</span><span class="n">req</span><span class="o">.</span><span class="n">http</span><span class="o">.</span><span class="n">host</span><span class="p">);</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">hash_data</span><span class="p">(</span><span class="n">server</span><span class="o">.</span><span class="n">ip</span><span class="p">);</span> <span class="p">}</span> <span class="k">return</span> <span class="p">(</span><span class="n">lookup</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>As you can see it first hashes <cite>req.url</cite> and then <cite>req.http.host</cite> if it exists. It is worth pointing out that Varnish doesn’t lowercase the hostname or the URL before hashing it so in theory having “Varnish.org/” and “varnish.org/” would result in different cache entries. Browsers however, tend to lowercase hostnames.</p> <p>You can change what goes into the hash. This way you can make Varnish serve up different content to different clients based on arbitrary criteria.</p> <p>Let’s say you want to serve pages in different languages to your users based on where their IP address is located. You would need some Vmod to get a country code and then put it into the hash. It might look like this.</p> <p>In <cite>vcl_recv</cite>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">http</span><span class="o">.</span><span class="n">X</span><span class="o">-</span><span class="n">Country</span><span class="o">-</span><span class="n">Code</span> <span class="o">=</span> <span class="n">geoip</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="n">client</span><span class="o">.</span><span class="n">ip</span><span class="p">);</span> </pre></div> </div> <p>And then add a <cite>vcl_hash</cite>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sub</span> <span class="n">vcl_hash</span> <span class="p">{</span> <span class="n">hash_data</span><span class="p">(</span><span class="n">req</span><span class="o">.</span><span class="n">http</span><span class="o">.</span><span class="n">X</span><span class="o">-</span><span class="n">Country</span><span class="o">-</span><span class="n">Code</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>Because there is no <cite>return(lookup)</cite>, the builtin VCL will take care of adding the URL, <cite>Host:</cite> or server IP# to the hash as usual.</p> <p>If <cite>vcl_hash</cite> did return, ie:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sub</span> <span class="n">vcl_hash</span> <span class="p">{</span> <span class="n">hash_data</span><span class="p">(</span><span class="n">req</span><span class="o">.</span><span class="n">http</span><span class="o">.</span><span class="n">X</span><span class="o">-</span><span class="n">Country</span><span class="o">-</span><span class="n">Code</span><span class="p">);</span> <span class="k">return</span><span class="p">(</span><span class="n">lookup</span><span class="p">);</span> <span class="p">}</span> </pre></div> </div> <p>then <em>only</em> the country-code would matter, and Varnish would return seemingly random objects, ignoring the URL, (but they would always have the correct <cite>X-Country-Code</cite>).</p> </div> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="vcl-backends.html" title="previous chapter">Backend servers</a></p> <h4>Next topic</h4> <p class="topless"><a href="vcl-grace.html" title="next chapter">Grace mode and keep</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/users-guide/vcl-hashing.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3 id="searchlabel">Quick search</h3> <div class="searchformwrapper"> <form class="search" action="../search.html" method="get"> <input type="text" name="q" aria-labelledby="searchlabel" /> <input type="submit" value="Go" /> </form> </div> </div> <script>$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="vcl-grace.html" title="Grace mode and keep" >next</a> |</li> <li class="right" > <a href="vcl-backends.html" title="Backend servers" >previous</a> |</li> <li class="nav-item nav-item-0"><a href="../index.html">Varnish version 7.1.1 documentation</a> »</li> <li class="nav-item nav-item-1"><a href="index.html" >The Varnish Users Guide</a> »</li> <li class="nav-item nav-item-2"><a href="vcl.html" >VCL - Varnish Configuration Language</a> »</li> <li class="nav-item nav-item-this"><a href="">Hashing</a></li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2010-2014, Varnish Software AS. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.4.3. </div> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 8.2.9 | Generation time: 0.1 |
proxy
|
phpinfo
|
Settings