Varnish (1) cache, proxy, and Server Load balancer

Source: Internet
Author: User
Tags varnish

Varnish is the cache (accelerator) and reverse proxy server of the Web server. It is lightweight and can carry many concurrent connections. For example, a Norwegian electronic news company replaced more than 10 squids with three varnish.

Varnish processes are roughly divided into two types:

1. Master processes (read configuration files, call suitable storage types, create or read cache files of the corresponding size, Manage Sub-processes, and initialize struct for managing the structure space (usually referred to used as memory segments for storage )) the main function is fork and monitor various child processes.

2. The child process (thread) maps cache files to the memory using MMAP (MMAP: maps a file or other objects to the memory, the file is mapped to multiple pages in the memory. After MMAP is used to map a file to a process, you can directly operate on this virtual address to perform read and write operations on the file, without calling system calls such as read and write. however, you must note that when writing this section of memory directly, content that exceeds the current file size will not be written .) create and initialize idle struct. The child process allocates a lot of worker threads to work. Many threads perform their respective jobs to complete related work. WORKER: process user requests accpet: accept user requests.

Varnish is also the mode in which a single worker thread responds to a user request.

Varnish pre-allocates idle process space (struct), and finds a struct with a relatively close size for storage. In addition, varnish not only supports memory caching, but also supports storing files on disks as cached objects.

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411632372yeTx.png "width =" 520 "Height =" 360 "/> left chart cache with black logo after hit, the system directly constructs a Response Message and sends it to the client, the pink logo indicates that after the cache miss, varnish re-constructs a new request to the back-end real server. Here, there is another very important step which is not shown in the figure, that is, when varnish constructs a new request to the backend server node, it will determine whether the new connection needs to be Cache.

Varnish requires a VCL compiler tool to explain the VCL statement for processing.

How to install varnish?

Because the RPM package is not available in the system disk base source, only the epel source is available, or the repo.varnish-cache.org/redhat/varnish 3.0/el6

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411632386sfyM.png "width =" 523 "Height =" 357 "/> RPM packages are available select installation. Here we recommend installing the RPM package for the varnish-3.0.el6 as well as the package for varnish-libs and varnish-docs.

Download and install Yum install. Pay attention to the following dependencies: GCC, CPP, PPL, mpfr, cloog-ppl.

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411633528597B.png "width =" 523 "Height =" 309 "/> the left diagram is the specific file, and common tools.

The VCL syntax format and meaning defined in the configuration file are as follows.

Example 1 650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657728D5GL.png "width =" 623 "Height =" 133 "/>

Example 2 650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657735JH6n.png "width =" 628 "Height =" 165 "/>

The defined proxy server is defined in/etc/Varnish/Default. VCL.

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657737K9cL.png "width =" 394 "Height =" 90 "/> and if you want repeat the configuration file, run the varnishadm command.

The command format is as follows: varnishadm-S/etc/Varnish/secret-T 127.0.0.1: 6082 (the default Management port defined in the configuration file)

However, you must enable the varnish service before starting the Service as follows:

[[Email protected] varnish] # service varnish start
Starting varnish cache: [OK]
[[Email protected] varnish] # varnishadm-S/etc/Varnish/secret-T 127.0.0.1: 6082
200
-----------------------------
Varnish cache CLI 1.0
-----------------------------
Linux, 2.6.32-431. el6.x86 _ 64, x86_64,-sfile,-smalloc,-hcritbit
Varnish-3.0.5 revision 1a89b1f

Type 'help' for command list.
Type 'quit' to close CLI session.

Varnish>

The interactive interface of the command line can be started.

Then I modified the configuration file as follows:

NOTE Information is removed separately:

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657751fAZ4.png "" 659 "Height =" 395 "/>

Use

Varnish> VCL. Load first1 (this name is defined by myself)./Default. VCL
200
VCL compiled.

Varnish> VCL. Use first1
200

Indicates that the configuration has taken effect. If you want to view the content, you can use VCL. Show.

If you want to check the details, change the logformat field in/etc/httpd/CONF/HTTP. conf of the server node

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657765Bvca.png "width =" 660 "Height =" 384 "/>

This figure shows that it is successful as a reverse proxy backend server of the cache server.

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657769tOiU.png "width =" 661 "Height =" 335 "/>

Varnish is renamed every time the configuration file is reloaded.

Scenario 1: If I want to add an item in the configuration, if it is a cache hit, I will add hit in the response information. If the cache does not hit, I will add miss.

Add the following information to the vcl_deliver of the configuration file:

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657771XKbr.png "width =" 654 "Height =" 213 "/>

Varnish> VCL. Load first2./Default. VCL
200
VCL compiled.
Varnish> VCL. Use first2
200

Then the client initiates the first request.

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657779lntD.png "width =" 657 "Height =" 252 "/>

The request hits again.

650) This. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657787bllt.png "width =" 659 "Height =" 217 "/>

Scenario 2: We have a server node named test1.html. What should we do if we don't want test1.html to be added to the cache?

It must be defined in vcl_recv.

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657788Zwem.png "" 659 "Height =" 102 "/>

Exact meaning matching. As long as/test1.html is shown in the URL, we can ignore caching. Then, enter

Varnish> VCL. Load first3./Default. VCL
200
VCL compiled.
Varnish> VCL. Use first3
200

The request initiated by the client to test1.html.

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_14116577979W8Q.png "" 660 "Height =" 546 "/> no matter how many requests are initiated.

Here we need to add a few built-in variables about varnish. These variables can only be used in vcl_hit or vcl_error, and most of them are read-only)

OBJ. Status

OBJ. Response

OBJ. TTL

OBJ. hits.

The configuration file defined by the system has one

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657799qJdY.png "" 646 "Height =" 303 "/>

How can I manually clear cache objects?

Use ACL to define purgers

Varnish> varnish> VCL. Load first4./Default. VCL
200
VCL compiled.
Varnish> VCL. Use first4
200

[[Email protected] HTML] # curl-x purge 172.16.24.100: 6081/index.html

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 strict // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<HTML>
<Head>
<Title> 200 purged successful</title>
</Head>
<Body>
<H1>Error 200 purged successful</H1>

Purged successful


<H3> guru meditation:

Xid: 183182795


<HR>

Varnish Cache Server


</Body>
</Html>
[[Email protected] HTML] #

How does varnish perform health check on backend server nodes?

Use the. Probe Method

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657801BW38.png "" 667 "Height =" 197 "/> you can perform health checks on the backend host.

Then

Varnish> VCL. Load first5./Default. VCL
200
VCL compiled.
Varnish> VCL. Use first5
200

Use varnishstat or type backend. List in the varnishadm command line.

Varnish> backend. List
200
Backend name refs Admin Probe
Default (172.16.24.101, 80) 6 probe healthy 8/8

Very healthy,

If the service of the backend server is stopped,

When I stop the service.

Varnish> backend. List
200
Backend name refs Admin Probe
Default (172.16.24.101, 80) 6 probe healthy 4/8

Detection started

Varnish> backend. List
200
Backend name refs Admin Probe
Default (172.16.24.101, 80) 6 probe sick 0/8

Finally, it is finished.

Varnish can also proxy multiple nodes. The following is a simple planning diagram:

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657805WQo5.png "" 545 "Height =" 315 "/>

In this step, you need to modify the configuration file varnish,/etc/Varnish/Default. VCL

Define the following information:

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_14116578118Wfc.png "" 529 "Height =" 444 "/> the scheduling method is round-robin, the figure shows the error round-robin.

In addition, you need to define set. backend = webs in vcl_recv.

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657815gPb4.png "" 532 "Height =" 242 "/>

VCL. Load first6./Default. VCL

VCL. Use first6

Then let the client start to initiate a request and give it a try:

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657818Ub2f.png "" 537 "Height =" 251 "/> web1 hard to refresh at this time will still be cached hit. What if we request a default page that is not index.html ?? Will Server Load balancer work?

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657821iHaA.png "" 532 "Height =" 310 "/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/25/6249823_1411657823gGvs.png "" 530 "Height =" 379 "/> is indeed a load balancing effect.

Use Param. Show in varnishadm to display parameters that can be adjusted in varnish. Param. Set you can directly set the syntax to Param. Set <param> <value>

Varnish (1) cache, proxy, and Server Load balancer

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.