Varnish Proxy Cache

Source: Internet
Author: User
Tags varnish

I. Basic varnish knowledge

How varnsih works

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-22611 "border =" 0 "alt =" wps_clip_image-22611 "src =" http://img1.51cto.com/attachment/201409/25/8400375_14116610955EEG.png "Height =" 353 "/>

1. How varnish works

The client request arrives at the varnish proxy. The accept in the Child thread receives the request process and submits it to worker threads for processing. worker threads first goes to object expiry to find the cache,

If no resource is found, go to the upstream server backend lcatinon to find the resource and return the varnish proxy to check whether the cache rules are met. If yes, the cache is cached. If not, the request is directly returned to the client.

2. cache Classification

Proxy Cache: the client requests the proxy and first looks for the cache. If the cache does not exist, the proxy finds the resource on the upstream server and caches it on the proxy, and then returns it to the client.

Bypass cache: the client goes to the cache to find the cache. If the cache does not hit the client, the client goes to the upstream server to find the resource and returns it to the local location. Then, the client caches the resource to the cache.

3. Applicable scenarios of memcache

Disadvantages of memcache: it cannot adapt to real-time updates. If real-time updates occur, the cache does not hit the cache and the hit rate is low.

Apsaradb for memcache supports distributed caching. memcache is not required for MySQL master-slave databases. memcache is suitable for multiple MySQL cluster environments. In this case, the query performance can be better directly obtained from the MySQL cache.

4. Functions of varnish status engines:

Vcl_recv: implements security policies. It only processes HTTP methods that can be identified and only caches get and head methods. It does not cache user-specific data (Cache policies based on client requests). vcl_fetch: cache vcl_pipe Based on the server response policy: used to send requests directly to the backend host; vcl_hash: the data source vcl_pass when custom hash is generated: Used to directly transfer requests to the backend host; vcl_hit: the operation to be performed when the cached object is found in the cache; vcl_miss: the operation to be performed when the cached object is found in the cache; vcl_deliver: the method used to respond the content requested by the user to the client; vcl_error: Cache Policy for merging error responses on the varnish side;

5. Principle of varnish Cache

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-25362 "border =" 0 "alt =" wps_clip_image-25362 "src =" http://img1.51cto.com/attachment/201409/25/8400375_1411661111P5RH.png "Height =" 532 "/>

Ii. varnish experiments

Node1 172.16.11.143 centos6.5 + Varnish

Node2 172.16.11.144 centos6.5 + HTTP

1. Software Installation

Listen 1rpm-IVH varnish-libs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm/etc/logrotate. d/varnish # Rolling log/etc/rc. d/init. d/varnish # service/etc/rc. d/init. d/varnishlog # log/var/lib/varnish # shared Cache/var/log/varnish # log storage/etc/Varnish/default. VCL # configuration file

2. Simple proxy configuration

Node1

Varnish_listen_port = bytes = 64 m # Set the cache size ### backend storage specification # varnish_storage = "file, $ {varnish_storage_file}, $ {varnish_storage_size}" varnish_storage = "malloc, $ {varnish_storage_size} "# What is the cache name Vim/etc/Varnish/default. vclbackend default {. host = "172.16.11.144 ";. port = "80";} service varnish restartss-tnl # Check whether the port is listening

Node2

Yum install httpd -yVim /var/www/html/index.html

3. Enable the backend server to view the client's access

Node1

CP/etc/Varnish/default. VCL/etc/Varnish/test. vclvim/etc/Varnish/test. vclsub vcl_recv {If (req. restarts = 0) {If (req. HTTP. x-forwarded-For) {set req. HTTP. x-forwarded-for = req. HTTP. x-forwarded-for + "," + client. IP;} else {set req. HTTP. x-forwarded-for = client. IP ;}} return (lookup);} [[email protected] ~] # Varnishadm-S/etc/Varnish/secret-T 127.0.0.1: 6082 varnish> VCL. list # view available 200 active 2 bootavailable 0 test1varnish> VCL. load test1 test1.vcl # compile 200 VCL compiled. varnish> VCL. use test1 # Use 200 varnish> VCL. list # view the availability of 200 available 2 bootactive 0 test1

Node2

Vim/etc/httpd/CONF/httpd. conflogformat "% {X-forwarded-for} I % L % u % t \" % R \ "%> S % B \" % {Referer} I \ "\" % {User-Agent} I \ "" combinedservice httpd reload ############################ ######################################## ################ test HTTP in the browser: // 172.16.11.143node2tail/var/log/httpd/access_log172.16.0.101--[04/SEP/2014: 16: 43: 27 + 0800] "Get/favicon. ico http/1.1 "404 288"-"" Mozilla/5.0 (Windows NT 6.1; wow64) applewebkit/537.36 (khtml, like gecko) Chrome/31.0.1650.63 Safari/537.36"

4. Set to check whether the cache is hit

Node1

Vim/etc/Varnish/test. vclsub vcl_deliver {If (obj. hits> 0) {set resp. HTTP. x-Cache = "hit via" + "" + server. hostname;} else {set resp. HTTP. x-Cache = "Miss via" + "" + server. hostname;} return (deliver);} [[email protected] ~] # Varnishadm-S/etc/Varnish/secret-T 127.0.0.1: 6082 varnish> VCL. load Test2 test1.vcl200 VCL compiled. varnish> VCL. use test2200 varnish> VCL. list200 available 0 bootavailable 2 test1active 0 Test2 browser access F12 View

5. Precisely set the page without caching

If (req. url ~"~ /Test.html $ ") {return (PASS)} compile and use the VCL. Load test3 test1.vclvcl. Use test2vcl. List browser to test

Http: // 172.16.11.143/test/index.html

Variable usage rules

6. the built-in variables are used in that status engine.

650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" wps_clip_image-14876 "border =" 0 "alt =" wps_clip_image-14876 "src =" http://img1.51cto.com/attachment/201409/25/8400375_1411661119MKSM.png "Height =" 308 "/>

7. varnish built-in variables:

The built-in variable available when the request arrives: req. urlreq. requestreq. HTTP. headerreq. restarts: Number of times the request is restarted; server. ipserver. portserver. hostnameclient. ipreq. the built-in variable bereq that can be used when backend requests to the backend host. urlbereq. requestbereq. HTTP. headerbereq. connect_timeoutbereq.proto is the built-in variable beresp that can be used to obtain the response object from the backend host. statusberesp. responseberesp. HTTP. headerberesp. ttlberesp. backend. nameberesp. backend. ipberesp. backend. port built-in variables available when the cached object enters the cache (only available for vcl_hit or vcl_error, and mostly read-only) obj. statusobj. responseobj. ttlobj. hitsobj. HTTP. the built-in variable resp available when the header responds to the client. protoresp. statusresp. responseresp. HTTP. header

8. Set the definition ACL to clear the cache

ACL purgers {"127.0.0.1"; "192.168.0.0"/24; # define which can be used to clear cache} sub vcl_recv {If (req. Request = "purge") {If (! Client. ip ~ Purgers) {error 405 "method not allowed";} # judge whether the IP address is an address in the ACL. If not, you do not need to search for the cache return (lookup ); # Is the address defined in the ACL to find the cache} # Set miss and hitsub vcl_hit {If (req. request = "purge") {purge; error 200 "purged" ;}# cache hit returns error200} sub vcl_miss {If (req. request = "purge") {purge; Error 404 "not in cache";} # The cache does not hit it with an error404} # sub vcl_pass {# If (req. request = "purge") {# error 502 "purge on a passed object "; #}#}#################################### ######################################## ######## compile and test curl-I http: // 172.16.11.143/index.html curl-x purge http: // 172.16.11.143/index.html # specify cache pruning hits

9. varnish Health Check

. URL: The request URL for detecting the health status. format of request content. window: the minimum number of resources to be checked. threshold: the minimum number of times a resource is detected from none to exist or from existence to none. initial: the minimum number of successful detection times required for the backend host when varnish is started. The default value is threshold. interval: the period of request sending. expected_response: Status Code of the expected host response. timeout: The expiration time of each probe request. The default value is 2 seconds.

Example

Define backend default {. host = "172.16.11.144 ";. port = "80 ";. probe = {. url = "/index.html ";. interval = 2 S ;. window = 8 ;. threshold = 2 ;} ######################################## ######################################## ##### compile and configure varnishstatbackend. list # Check the health status. We can stop HTTP at the backend. Check, enable, and view the health status.

10. Define polling

Add a node

Node3 172.16.11.145 centos6.5 + HTTP

backend web1 {  .host = "172.16.11.144";  .port = "80";  .probe = {        .url = "/index.html";        .interval = 2s;        .window = 8;        .threshold = 2;}}backend web2 {  .host = "172.16.11.145";  .port = "80";  .probe = {        .url = "/index.html";        .interval = 2s;        .window = 8;        .threshold = 2;}}director webservers round-robin {        { .backend = web1; }        { .backend = web2; }}#         return (pass);#     }    set req.backend = webservers;    return (lookup);}[[email protected] ~]# varnishadm  -S /etc/varnish/secret -T 127.0.0.1:6082varnish> varnish> vcl.load test9 test1.vcl200       VCL compiled.varnish> vcl.use test9200       varnish> backend.list200       Backend name                   Refs   Admin      Probedefault(172.16.11.144,,80)     5      probe      Healthy (no probe)web1(172.16.11.144,,80)        1      probe      Healthy 8/8web2(172.16.11.145,,80)        1      probe      Healthy 8/8

You can also go to the web page to test

Http: // 172.16.11.143

Varnish Proxy Cache

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.