Varnish configuration language-vcl (Varnish config language-vcl)

Source: Internet
Author: User
Tags data structures varnish

http://linuxguest.blog.51cto.com/195664/355220/

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://linuxguest.blog.51cto.com/195664/355220Varnish Configuration LANGUAGE-VCL (Varnish Configuration Language-VCL)          Varnish has a great configuration system, and most of the other systems use configuration instructions that let you turn on or off some switches.          Varnish uses the regional configuration language, which is called "VCL" (Varnish configuration language), and when the VCL is executed, the varnish transforms the VCL into binary code.      The VCL file is divided into subroutines, and different subroutines are executed at different times, such as when a subroutine executes on request, and another subroutine executes when it receives a file sent by the backend server. Varnish will execute its subroutine code at different stages because its code is executed one line at a time and there are no priority issues.        You can call the function in this subroutine at any time and exit when he is done.        If in the end you did not invoke the functionality in your subprocess, varnish will execute some of the built-in VCL code, which is the code that is commented in DEFAULT.VCL. 99% chance you need to change the two sub-processes of VCL_RECV and Vcl_fetch.Vcl_recv               VCL_RECV (Of course, we are a bit short on the character set, it should be Unix) at the beginning of the request is called, after receiving, parsing, decide whether to respond to the request, how to respond, which background server to use.               In Vcl_recv, you can modify the request, for example, you can modify the cookie, add or delete the requested header information. Note Only the requested target in Vcl_recv, req is available.Vcl_fetch        Vcl_fetch is called when a file is successfully retrieved from the background, and his task is to change the response header file, triggering the ESI process to poll for failed requests in the background server. The same as in Vcl_fetch contains the target of the request, req,available, where is usually backend Response,beresp.beresp will contain the header information of the backend server's HTTPActions         mainly has the following actions                pass               \\ when a request is passed, the request will be forwarded to the backend server via varnish, but it will not be cached. The pass can be placed in vcl_recv  and  vcl_fetch.               lookup            \\ when a request is lookup in Vcl_recv, varnish extracts the data from the cache, and if there is no data in the cache, it will be set to pass , you cannot set the lookup in Vcl_fetch.               pipe                \\pipe, similar to pass, accesses back-end servers, but when you enter pipe mode, Before this connection is closed, all subsequent requests are sent to the backend server (this sentence is simplified after my own understanding, the ability of friends can look at the official documents, give me suggestions for changes).               deliver    The target of the         \\ request is cached and then sent to the guestClient               esi            \\ESI-process  the fetched document (which I understand is the replacement of an HTML code in the VCL)         Requests,responses and Objects               In VCL, there are 3 important data structures               request   Come in         responses  from the back-end server                object  stored in cache                 in VCL, you need to know the following structure                req          \\ requests the target, when Varnish receives a request, the Req object is created, and most of your work in VCL_RECV is done on the Req object.               beresp    The        \\ backend server returns the target, which contains the header information returned, and most of your work in Vcl_fetch is carried out on the Beresp object.               obj          \\ is the target of the cache, the read-only target is saved in memory, and objThe value of the. TTL can be modified, and the other can be read only.         Operaors               VCL support operators, please read the following example:                =                    \\ Assignment Operators                ==                  \\ contrast                ~                    \\ matches, can be used in ACLs and regular expressions                .                   \\ Negative             &NBsp; &&                  \\ Logic and               | |                     \\ logic or               example 1–manipulation Headers                We want to cancel our server/ All caches under the images directory: Sub vcl_recv {if (req.url ~ "^/images") {unset Req.http.cookie;} Now, when this request is operating on the backend server, there will be no cookie header, and here the interesting line Is  if-statement, which matches the URL, and if this action is matched, the cookie in the header message is deleted.        example 2–manipulation Beresp                 returns the value of an object from the back-end server to meet some criteria, we modify its TTL value: Sub Vcl_fetch {    if (beresp.url ~ "\. ( png|gif|jpg) ($ ") {      unset Beresp.http.set-cookie;      beresp.ttl = 3600;   }}        example3-acls                 you create a VCL keyword for the access control list. You can configure the IP address of the client                # who is allowed To purge ....                ACL Local {                     " localhost ";                     " 192.168.1.0 "/24; /* And everyone on the local network */                    ! "192.168.1.23"; /* Except for the Dialin router */         &Nbsp;    }                    sub VCL_RECV {                    if (Req.request = = "PURGE") {                    if (client.ip ~ local) {                       return (lookup);                    }                  }                }                           & nbsp;   Sub Vcl_hit {                  if (Req.request = = "PURGE") {                     set obj.ttl = 0s;                      error "purged";                    }               }                                Sub Vcl_miss {                  if (Req.request = = "PURGE") {                    error 404 "Not in 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.