Varnish application and instance details

Source: Internet
Author: User
Tags varnish


Varnish is an upgraded version of squid. It is mainly used for reverse proxy of HTTP and HTTP cache to provide acceleration.
1. How varnish works: Each thread responds to a user request (when a user request arrives, accept receives the request and assigns it to an idle Worker Process for processing,
(Read from the cache by the worker thread according to the requested URL. If the requested URL can be found in the cache, the content is directly returned to the customer. If the content cannot be found in the cache, it is responsible for finding data on the backend server. After finding the data, according to the cache mechanism, if the data can be cached, It is cached locally and then returned to the client .))

2. How many user requests can varnish respond? Depends on the total number of opened threads. What is used to control the total number of threads ??
The thread pool mechanism is used to control opening multiple threads to respond to client requests. (Generally, the number of thread pools opened is proportional to the number of cores on the physical machine. The number of physical cores cannot exceed .)

3. varnish cache method: (the cache can also be stored in memory and disk)
Cache data storage method: the cache space is allocated with a fixed size. If you want to cache a data, cache it to the space closest to the cache data size based on the cache data size.
If the cache space is slow, the LRU mechanism will be used to replace the memory space (LRU is the least recently used at least the algorithm. Memory Management is a page replacement algorithm. For data blocks (memory blocks) that are not used in the memory, they are called LRU, oracle moves data out of memory based on which data belongs to LRU to free up space for loading additional data .)

4. varnish: varinish manages the cache timeout period thread TTL (if the TTL value exceeds the set value, the cache space will be released, it is automatically merged into a large free space, avoiding space waste and space fragmentation (memory merging mechanism ).
Varnish installation Website: www.varnish-cache.org/releases
Http://repo.varnish-cache.org/redhat/varnish3.0/el6/
Pack: varnish-3.0.5-1.el6.x86_64.rpm
Varnish-docs-3.0.5-1.el6.x86_64.rpm
Varnish-libs-3.0.5-1.el6.x86_64.rpm

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" Topology "border =" 0 "alt =" Topology "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631676CLf3.jpg "860" Height = "358"/>

Lab environment:
Vm1-web1: 172.16.3.1/16
Vm2-web2: 172.16.3.2/16
Vm3-varnish: 172.16.3.10/16
Configuration:
Vm1-web1:
# Yum install httpd
# Service httpd start
# Echo "172.16.3.1">/var/www/html/index.html

Vm2-web2:
# Yum install httpd
# Service httpd start
# Echo "172.16.3.2">/var/www/html/index.html
Vm3-varnish:
# Yum install Varnish
Global Property configuration file:
# Cat/etc/sysconfig/Varnish
Nfiles = 131072 maximum number of files allowed to be opened

Memlock = 82000 the default log size is 82 MB.

Varnish_vcl_conf =/etc/Varnish/Default. VCL specifies the varnish configuration file

Varnish_listen_port = 80 specifies the listening port

Varnish_admin_listen_address = 127.0.0.1 specifies the management host
Varnish_admin_listen_port = 6082 specifies the port number of the Management host.

Varnish_secret_file =/etc/Varnish/secret specifies the directory of the key file
Varnish_min_threads = 50 minimum number of threads enabled by a worker by default

Varnish_max_threads = 1000 maximum number of threads enabled by a worker

Varnish_thread_timeout = 120 how long a worker is idle will be terminated in seconds

Varnish_storage_file =/var/lib/Varnish/varnish_storage.bin specify the location of the cached File

Varnish_storage_size = 1g the maximum cache file size is 1g

Varnish_storage = "file, $ {varnish_storage_file}, $ {varnish_storage_size}" storage file Definition Format

Varnish_ttl = 120 cache reclaim cycle time.

Master configuration file configuration
# Cp/etc/Varnish/Default. VCL/etc/Varnish/test1.vcl)
# Vim/etc/Varnish/test1.vcl

Backend web1 {define the first backend-Server
. Host = "172.16.3.1"; backend server address
. Port = "80"; listening port
. Probe = {monitoring Detection Mechanism
. Url = "/index.html"; detect by URL
. Interval = 2 S; detected every 2 seconds
. Window = 8; from normal to failure detection 8 times
. Threshold = 2; 8 if two of them are monitored, it means they are monitored.
}
}
Backend web2 {define the second backend-Server
. Host = "172.16.3.2 ";
. Port = "80 ";
. Probe = {
. Url = "/index.html ";
. Interval = 2 S;
. Window = 8;
. Threshold = 2;
}
}
Director webservers round-robin {defines the backend server group webservers and specifies the access mechanism: here the round-robin mechanism is specified.
{. Backend = web1;} specifies the backend server.
{. Backend = web2 ;}
}
ACL purgers {define to clear the Control List of a single cache (Allowed Users)
"127.0.0.1 ";
"172.16.0.0"/16;
}

Sub vcl_recv {specify some access mechanisms of the receiving Client
Set Req. backend = webservers; application backend server

If (req. restarts = 0) {if the number of request restarts is 0
If (req. http. X-forwarded-For) {If X-forwarded-for exists
Set Req. http. X-forwarded-for =
Req. http. X-forwarded-for + "," + client. IP; then add the Client IP address after X-forwarded-
} Else {
Set Req. http. X-forwarded-for = client. IP; otherwise, set X-forwarded-for to the Client IP address.
}
}
If (req. url ~ "^/Test1.html $") {if the request is test1.html, skip the request directly to the backend server.
Return (PASS );
}

If (req. Request = "purge") {defines instructions for clearing the returned information of a single cache
If (client. IP !~ Purgers ){
Error 405 "method not allower .";
}
Return (lookup); if the customer request is purge and the client IP address is not purgers, the response method not allower.
}
Return (lookup); if the client request is purge, directly search for the cache
}

Sub vcl_hit {if it can be found in the cache, clear the cache directly and respond to "purged
If (req. Request = "purge "){
Purge;
Error 200 "purged ";
}
Return (deliver );
}
Sub vcl_miss {If no result is found in the cache, the not in purged is returned directly.
If (req. Request = "purge "){
Purge;
Error 404 "not in purged ";
}
Return (FETCH );
}

Sub vcl_fetch {the definition is not rigorous here (for convenience of testing, the periodic cache time can be up to 5 seconds)
Set beresp. TTL = 5S;
}

Sub vcl_deliver {set the response client Mechanism
If (obj. Hits> 0 ){
Set resp. http. X-Cache = "hit via" + "" + server. hostname; if the cache hits more than 0, add the Host Name of the varnish server in the response header,
} Else {
Set resp. HTTP. x-Cache = "Miss via" + "" + server. hostname; if the cache is not hit, add X-Cache = "Miss via" + "" + server in the response header. hostname

}
Return (deliver); the number of previous restarts.
}

Start the varnish service:
1. Service varnish start
2. # varnishadm-S/etc/Varnish/secret-T 127.0.0.1: 6082
VCL. Load Ning test1.vcl edit the effective configuration
VCL. Use Ning application configuration
========================================================== ========================================================== =
Test:
1. test whether the backend server can be trained in turn. It can also be determined that the cache will fail once every 5 seconds (the truth is shown in the figure)

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" 88 "border =" 0 "alt =" 88 "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631677mnZu.jpg "" 461 "Height =" 104 "/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" training 1 "border =" 0 "alt =" training 1 "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631677TfY3.jpg "532" Height = "247"/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" Rotation 2 "border =" 0 "alt =" Rotation 2 "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631677ryzl.jpg "435" Height = "223"/>
2. Check whether the backend server can be monitored properly)

Stop the backend server and check the process

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" failed detection process "border =" 0 "alt =" failed detection process "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631677Asbm.jpg "476" Height = "435"/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" monitoring detector drawing "border =" 0 "alt =" monitoring detector drawing "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631679DN9l.jpg "574" Height = "367"/>


3. Check whether the specified information is added to the Response Header (the image has the truth)

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" Hit 1 "border =" 0 "alt =" Hit 1 "src =" http://img1.51cto.com/attachment/201409/25/1384120_14116316804y8v.jpg "627" Height = "680"/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" hit 2 "border =" 0 "alt =" hit 2 "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631682WBFy.jpg "664" Height = "705"/>

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "Title =" hit 3 "border =" 0 "alt =" hit 3 "src =" http://img1.51cto.com/attachment/201409/25/1384120_1411631684reme.jpg "665" Height = "688"/>

 

4. Command for clearing a single cache:

# Curl-x purge http: // 172.16.3.10/index.html (command for clearing cache objects)
<? 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 </title>
</Head>
<Body>
<H1> error 200 purged

Purged


<H3> guru meditation:

Xid: 202759686


<HR>

Varnish Cache Server


</Body>
</Html>

# Curl http: // 172.16.3.10/index.html-I
HTTP/1.1 200 OK
Server: Apache/2.2.15 (centos)
Last-modified: Tue, 02 Sep 2014 08:08:58 GMT
Etag: "80102-c-50210a1a7a078"
Content-Type: text/html; charset = UTF-8
Content-Length: 12
Accept-ranges: bytes
Date: Tue, 23 Sep 2014 15:38:15 GMT
X-varnish: 202759707
Age: 0
Via: 1.1 Varnish
Connection: keep-alive
X-Cache: Miss via localhost. localdomain command. No hit is displayed.

Varnish management tools:

# Arnishadm-H query help information
# Varnishadm-S/etc/Varnish/secret-T 127.0.0.1: 6082 varnishadm management connection command

# Varnishstat detection hit rate
# Varnishlog monitoring log information from time to time
# The log recorded by varnishncsa is similar to that recorded by httpd.
172.16.20.109-[24/SEP/2014: 01: 45: 28 + 0800] "get http: // 172.16.3.10/HTTP/1.1 "200 11"-"" Mozilla/5.0 (Windows NT 6.1; wow64) applewebkit/537.36 (khtml, like gecko) Chrome/36.0.1985.125 Safari/537.36"

# The whole process of varnishhist request history
# Varnishtop sort requests (the largest number of requests is listed above)

Varnish application and instance details

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.