Varnish 4.0 Official document translation 16-backend servers/m/b/d/h

Source: Internet
Author: User
Tags varnish

Backend servers

Varnish has the concept of "backend" or "source" server. Backend server provides varnish-accelerated content.

The first step is to tell varnish where to find the backend server. Use your favorite editor to open the VCL file introduced by Varnishd.

A small paragraph at the beginning of a VCL file looks something like this:

# backend Default {#. host = "127.0.0.1"; #. Port = "8080"; #}

After removing comments

Backend default {. Host = "127.0.0.1"; . Port = "8080";}

Such a configuration defines a backend in varnish, which is called default. (and C's function definition is a bit similar), when varnish needs to get content from the backend, it will connect to 127.0.0.1 's 8080 port.

Varnish can define multiple back-end, or it can put several back-end in a back-end cluster that has already achieved load balancing.

Multiple backends

In some cases you may need to let varnish cache multiple backend content. You might want to have varnish map all the URLs on a single back end, or multiple back ends. Here are some parameters to meet this requirement.

Now we need to introduce Java applications to the PHP site. The links to Java Apps start with/java/. The server that handles the Java application listens on port 8000. The default DEFAULT.VCL file is this:

Backend default {. Host = "127.0.0.1"; . Port = "8080";}

Add a back end:

Backend java {. host = "127.0.0.1"; . Port = "8000";}

Now you need to tell varnish to send different URLs to different backend servers. In the VCL_RECV:

Sub Vcl_recv {if (req.url ~ "^/java/") {set req.backend_hint = Java;    } else {Set req.backend_hint = default; }}

It's really simple. Stop to think. As you can see, you could define how to choose the backend based on the actual arbitrary data. Do you want to send a mobile device request to a different backend? No problem. can meet your needs.

if (req.http.user-agent ~ "(? i) midp| wap| Up. browser| smartphone| obigo| mobile| AU. Browser|wxd. mms| wxdb.browser| cldc| Up. link|km. browser| ucweb| semc\-browser| mini| symbian| palm| nokia| panasonic| Mot| sonyericsson| nec| alcatel| ericsson| benq| benq| amoisonic| amoi| capitel| philips| Samsung| lenovo| mitsu| motorola| sharp| wapper| Lg| eg900| Cect| compal|kejian| bird| bird| g900/v1.0| arima| ctl| Tdg| daxian| daxian| dbtel| Eastcom| Eastcom| pantech| dopod| haier| haier| konka| kejian| lenovo| soutec| soutec| sagem| sec| sed| emol| inno55| zte|iphone| android| Windows ce| Wget| java| Opera ") {Set req.backend_hint = Mobile;}
Backends and virtual hosts in Varnish

Varnish fully supports virtual hosts. They have never explicitly declared this because varnish does this in an unconventional way. In VCL_RECV we set the route that handles HTTP requests. If you want the route to do something based on a basic virtual host, you just need to check the req.http.host.

Sub Vcl_recv {if (req.http.host ~ "foo.com") {set req.backend_hint = foo;    } elsif (req.http.host ~ "bar.com") {set req.backend_hint = bar; }}

~ After the symbol is understood as a regular expression. Here you can match "foo.com", "www.foo.com", "zoop.foo.com.cn" and other domain names that contain foo.com. The example is intentionally written so that if you clear your domain name, you need to use = = to replace the ~ symbol.

Sub Vcl_recv {if (Req.http.host = = "Foo.com" | | req.http.host = = "Www.foo.com") {set req.backend_hint = foo; }}
Directors

You can also make several back ends into a group of back ends. This group is called directors. Can improve performance and resiliency (add an app, or deactivate an app, and so on).

You can define several backend and then organize them in a directors. These actions require you to load vmod (varnish module) and call this vmod in Vcl_init.

Import directors; # load the directorsbackend server1 {. host = "192.168.0.10";} Backend Server2 {. host = "192.168.0.10";}    Sub Vcl_init {New bar = Directors.round_robin ();    Bar.add_backend (Server1); Bar.add_backend (Server2);} Sub Vcl_recv {# Send all traffic to the bar director:set req.backend_hint = Bar.backend ();}

The scheduler is the Round-robin scheduler. This means that directors will use the polling algorithm to distribute the request. There is also a random allocation request for the Director, which you guessed right, randomly.

But what if one of the servers goes down? Varnish can you distribute all your requests to a healthy server? Of course! The health checks is doing it!

Health Checks

Now let's set up two backend and health checks for a scheduler. Define the back end first:

backend server1 {    .host =  "Server1.example.com";     .probe = {        .url =  "/";         .timeout = 1s;        . interval = 5s;        .window = 5;         .threshold = 3;    }}backend server2  {    .host =  "server2.example.com";     .probe =  {        .url =  "/";         .timeout = 1s;        .interval =  5s;        .window = 5;         .threshold  = 3;    }} 

The. Probe is the new parameter. In the example above, varnish will detect the backend for every 5s, and the timeout is set to 1s. Each detection will send a get/request. If more than 3 of the 5 tests are successful, Varnish thinks the back end is healthy, and conversely, the backend has a problem.

More. Probe information can be viewed in varnish probes chapters

We now define the director

Import directors;sub vcl_init {New vdir = Directors.round_robin ();    Vdir.add_backend (Server1); Vdir.add_backend (Server2);}

You use the Vir director as the backend for processing requests, just as you would with a simple back-end. Varnish does not send requests to the backend marked unhealthy.

If all the back ends are down, varnish can also return content that has expired for the user. See misbehaving servers For more information on how to turn this feature on.

Please note that Varnish will keep the probe running healthy for all loaded vcls. Varnish the probe into a seemingly completely integrated-so if you have a lot of loaded VCL, do not modify the probe configuration, uninstalling VCL disables probes. For more information, see Ref:reference-vcl-director.


Varnish 4.0 Official document translation 16-backend servers/m/b/d/h

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.