HEX
Server: Apache
System: Linux vpshost0650.publiccloud.com.br 4.4.79-grsec-1.lc.x86_64 #1 SMP Wed Aug 2 14:18:21 -03 2017 x86_64
User: bandeirantesbomb3 (10068)
PHP: 8.0.7
Disabled: apache_child_terminate,dl,escapeshellarg,escapeshellcmd,exec,link,mail,openlog,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,php_check_syntax,php_strip_whitespace,popen,proc_close,proc_open,shell_exec,symlink,system
Upload Files
File: //proc/thread-self/root/usr/share/doc/varnish-6.0.3/html/users-guide/vcl-backends.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Backend servers &#8212; Varnish version 6.0.3 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" 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="Hashing" href="vcl-hashing.html" />
    <link rel="prev" title="actions" href="vcl-actions.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-hashing.html" title="Hashing"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="vcl-actions.html" title="actions"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Varnish version 6.0.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >The Varnish Users Guide</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="vcl.html" accesskey="U">VCL - Varnish Configuration Language</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="backend-servers">
<span id="users-guide-backend-servers"></span><h1>Backend servers<a class="headerlink" href="#backend-servers" title="Permalink to this headline">¶</a></h1>
<p>Varnish has a concept of “backend” or “origin” servers. A backend
server is the server providing the content Varnish will accelerate.</p>
<p>Our first task is to tell Varnish where it can find its backends. Start
your favorite text editor and open the relevant VCL file.</p>
<p>Somewhere in the top there will be a section that looks a bit like this.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># backend default {</span>
<span class="c1">#     .host = &quot;127.0.0.1&quot;;</span>
<span class="c1">#     .port = &quot;8080&quot;;</span>
<span class="c1"># }</span>
</pre></div>
</div>
<p>We remove the comment markings in this text stanza making the it look like.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">backend</span> <span class="n">default</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;127.0.0.1&quot;</span><span class="p">;</span>
    <span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="s2">&quot;8080&quot;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Now, this piece of configuration defines a backend in Varnish called
<em>default</em>. When Varnish needs to get content from this backend it will
connect to port 8080 on localhost (127.0.0.1).</p>
<p>Varnish can have several backends defined you can even join
several backends together into clusters of backends for load balancing
purposes.</p>
</div>
<div class="section" id="multiple-backends">
<h1>Multiple backends<a class="headerlink" href="#multiple-backends" title="Permalink to this headline">¶</a></h1>
<p>At some point you might need Varnish to cache content from several
servers. You might want Varnish to map all the URL into one single
host or not. There are lot of options.</p>
<p>Lets say we need to introduce a Java application into out PHP web
site. Lets say our Java application should handle URL beginning with
<cite>/java/</cite>.</p>
<p>We manage to get the thing up and running on port 8000. Now, lets have
a look at the <cite>default.vcl</cite>.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">backend</span> <span class="n">default</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;127.0.0.1&quot;</span><span class="p">;</span>
    <span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="s2">&quot;8080&quot;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>We add a new backend.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">backend</span> <span class="n">java</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;127.0.0.1&quot;</span><span class="p">;</span>
    <span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="s2">&quot;8000&quot;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Now we need tell Varnish where to send the difference URL. Lets look at <cite>vcl_recv</cite>.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sub</span> <span class="n">vcl_recv</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">url</span> <span class="o">~</span> <span class="s2">&quot;^/java/&quot;</span><span class="p">)</span> <span class="p">{</span>
        <span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">backend_hint</span> <span class="o">=</span> <span class="n">java</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">backend_hint</span> <span class="o">=</span> <span class="n">default</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>It’s quite simple, really. Lets stop and think about this for a
moment. As you can see you can define how you choose backends based on
really arbitrary data. You want to send mobile devices to a different
backend? No problem. <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">(req.http.User-agent</span> <span class="pre">~</span> <span class="pre">/mobile/)</span> <span class="pre">..</span></code> should do the
trick.</p>
<p>Without an explicit backend selection, varnish will continue using
the <cite>default</cite> backend. If there is no backend named <cite>default</cite>, the
first backend found in the vcl will be used as the default backend.</p>
</div>
<div class="section" id="backends-and-virtual-hosts-in-varnish">
<h1>Backends and virtual hosts in Varnish<a class="headerlink" href="#backends-and-virtual-hosts-in-varnish" title="Permalink to this headline">¶</a></h1>
<p>Varnish fully supports virtual hosts. They might however work in a somewhat
counter-intuitive fashion since they are never declared
explicitly. You set up the routing of incoming HTTP requests in
<cite>vcl_recv</cite>. If you want this routing to be done on the basis of virtual
hosts you just need to inspect <cite>req.http.host</cite>.</p>
<p>You can have something like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sub</span> <span class="n">vcl_recv</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="o">~</span> <span class="s2">&quot;foo.com&quot;</span><span class="p">)</span> <span class="p">{</span>
        <span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">backend_hint</span> <span class="o">=</span> <span class="n">foo</span><span class="p">;</span>
    <span class="p">}</span> <span class="n">elsif</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="o">~</span> <span class="s2">&quot;bar.com&quot;</span><span class="p">)</span> <span class="p">{</span>
        <span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">backend_hint</span> <span class="o">=</span> <span class="n">bar</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Note that the first regular expressions will match “foo.com”,
“www.foo.com”, “zoop.foo.com” and any other host ending in “foo.com”. In
this example this is intentional but you might want it to be a bit
more tight, maybe relying on the <code class="docutils literal notranslate"><span class="pre">==</span></code> operator in stead, like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sub</span> <span class="n">vcl_recv</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="o">==</span> <span class="s2">&quot;foo.com&quot;</span> <span class="o">||</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="o">==</span> <span class="s2">&quot;www.foo.com&quot;</span><span class="p">)</span> <span class="p">{</span>
        <span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">backend_hint</span> <span class="o">=</span> <span class="n">foo</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="directors">
<span id="users-guide-advanced-backend-servers-directors"></span><h1>Directors<a class="headerlink" href="#directors" title="Permalink to this headline">¶</a></h1>
<p>You can also group several backend into a group of backends. These
groups are called directors. This will give you increased performance
and resilience.</p>
<p>You can define several backends and group them together in a
director. This requires you to load a VMOD, a Varnish module, and then to
call certain actions in <cite>vcl_init</cite>.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">directors</span><span class="p">;</span>    <span class="c1"># load the directors</span>

<span class="n">backend</span> <span class="n">server1</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;192.168.0.10&quot;</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">backend</span> <span class="n">server2</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;192.168.0.10&quot;</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">sub</span> <span class="n">vcl_init</span> <span class="p">{</span>
    <span class="n">new</span> <span class="n">bar</span> <span class="o">=</span> <span class="n">directors</span><span class="o">.</span><span class="n">round_robin</span><span class="p">();</span>
    <span class="n">bar</span><span class="o">.</span><span class="n">add_backend</span><span class="p">(</span><span class="n">server1</span><span class="p">);</span>
    <span class="n">bar</span><span class="o">.</span><span class="n">add_backend</span><span class="p">(</span><span class="n">server2</span><span class="p">);</span>
<span class="p">}</span>

<span class="n">sub</span> <span class="n">vcl_recv</span> <span class="p">{</span>
    <span class="c1"># send all traffic to the bar director:</span>
    <span class="nb">set</span> <span class="n">req</span><span class="o">.</span><span class="n">backend_hint</span> <span class="o">=</span> <span class="n">bar</span><span class="o">.</span><span class="n">backend</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This director is a round-robin director. This means the director will
distribute the incoming requests on a round-robin basis. There is
also a <em>random</em> director which distributes requests in a, you guessed it,
random fashion. If that is not enough, you can also write your own director
(see <a class="reference internal" href="../reference/directors.html#ref-writing-a-director"><span class="std std-ref">Writing a Director</span></a>).</p>
<p>But what if one of your servers goes down? Can Varnish direct all the
requests to the healthy server? Sure it can. This is where the Health
Checks come into play.</p>
</div>
<div class="section" id="health-checks">
<span id="users-guide-advanced-backend-servers-health"></span><h1>Health checks<a class="headerlink" href="#health-checks" title="Permalink to this headline">¶</a></h1>
<p>Lets set up a director with two backends and health checks. First let
us define the backends:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">backend</span> <span class="n">server1</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;server1.example.com&quot;</span><span class="p">;</span>
    <span class="o">.</span><span class="n">probe</span> <span class="o">=</span> <span class="p">{</span>
        <span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;/&quot;</span><span class="p">;</span>
        <span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="mi">1</span><span class="n">s</span><span class="p">;</span>
        <span class="o">.</span><span class="n">interval</span> <span class="o">=</span> <span class="mi">5</span><span class="n">s</span><span class="p">;</span>
        <span class="o">.</span><span class="n">window</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
        <span class="o">.</span><span class="n">threshold</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="n">backend</span> <span class="n">server2</span> <span class="p">{</span>
    <span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="s2">&quot;server2.example.com&quot;</span><span class="p">;</span>
    <span class="o">.</span><span class="n">probe</span> <span class="o">=</span> <span class="p">{</span>
        <span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;/&quot;</span><span class="p">;</span>
        <span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="mi">1</span><span class="n">s</span><span class="p">;</span>
        <span class="o">.</span><span class="n">interval</span> <span class="o">=</span> <span class="mi">5</span><span class="n">s</span><span class="p">;</span>
        <span class="o">.</span><span class="n">window</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
        <span class="o">.</span><span class="n">threshold</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>What is new here is the <code class="docutils literal notranslate"><span class="pre">probe</span></code>.  In this example Varnish will check the
health of each backend every 5 seconds, timing out after 1 second. Each
poll will send a GET request to /. If 3 out of the last 5 polls succeeded
the backend is considered healthy, otherwise it will be marked as sick.</p>
<p>Refer to the <a class="reference internal" href="../reference/vcl.html#reference-vcl-probes"><span class="std std-ref">Probes</span></a> section in the
<a class="reference internal" href="../reference/vcl.html#vcl-7"><span class="std std-ref">VCL</span></a> documentation for more information.</p>
<p>Now we define the ‘director’:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">directors</span><span class="p">;</span>

<span class="n">sub</span> <span class="n">vcl_init</span> <span class="p">{</span>
    <span class="n">new</span> <span class="n">vdir</span> <span class="o">=</span> <span class="n">directors</span><span class="o">.</span><span class="n">round_robin</span><span class="p">();</span>
    <span class="n">vdir</span><span class="o">.</span><span class="n">add_backend</span><span class="p">(</span><span class="n">server1</span><span class="p">);</span>
    <span class="n">vdir</span><span class="o">.</span><span class="n">add_backend</span><span class="p">(</span><span class="n">server2</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You use this <cite>vdir</cite> director as a backend_hint for requests, just like
you would with a simple backend. Varnish will not send traffic to hosts
that are marked as unhealthy.</p>
<p>Varnish can also serve stale content if all the backends are down. See
<a class="reference internal" href="vcl-grace.html#users-guide-handling-misbehaving-servers"><span class="std std-ref">Grace mode and keep</span></a> for more information on
how to enable this.</p>
<p>Please note that Varnish will keep health probes running for all loaded
VCLs. Varnish will coalesce probes that seem identical - so be careful
not to change the probe config if you do a lot of VCL loading. Unloading
the VCL will discard the probes. For more information on how to do this
please see ref:<cite>reference-vcl-director</cite>.</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Backend servers</a></li>
<li><a class="reference internal" href="#multiple-backends">Multiple backends</a></li>
<li><a class="reference internal" href="#backends-and-virtual-hosts-in-varnish">Backends and virtual hosts in Varnish</a></li>
<li><a class="reference internal" href="#directors">Directors</a></li>
<li><a class="reference internal" href="#health-checks">Health checks</a></li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="vcl-actions.html"
                        title="previous chapter">actions</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="vcl-hashing.html"
                        title="next chapter">Hashing</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-backends.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#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-hashing.html" title="Hashing"
             >next</a> |</li>
        <li class="right" >
          <a href="vcl-actions.html" title="actions"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Varnish version 6.0.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >The Varnish Users Guide</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="vcl.html" >VCL - Varnish Configuration Language</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2010-2014, Varnish Software AS.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.6.
    </div>
  </body>
</html>