Architect diary-VCL variables and common fragments

In VCL, there are 3 important data structures:

1. req : request target. When Varnish receives a request, this is the req object is created. Most of the work in vcl_recv is carried out on the req object.
2. beresp : The target returned by the back-end server, which contains the returned header information. Most of the work in vcl_fetch is carried out on beresp onject.
3.obj : the cached target, the read-only target is stored in the memory, the value of obj.ttl can be modified, the others can only be read

Since the subroutine has no parameters, the information necessary for the subroutine is handled through global variables.

The following variables are available everywhere:

  • now: the current time
    the following variables are valid in the backend stated:
  • .host: a backend host name or IP address
  • .port: a backend service name or the port number of
    the following variables are valid in processing the request:
  • cient.ip: client IP
  • cient.identity: the id of the client, used as the client director for load balancing
  • server.hostname: the host name of the server
  • server.identity: The identity of the server, set with the -i parameter. If the -i parameter is not passed to varnishd, server.identity will set the name for the varnishd instance.
  • server.ip: The client connected to the socket and received the IP address
  • server.port: The port number received when the client connects to the socket
  • req.request: request type, such as "GET", "HEAD"
  • req.proto: HTTP protocol of the client
  • req.url: requested URL
  • req.backend: which backend server is used to service this request
  • req.backend.healthy: Whether the backend server is healthy
  • req.http. Specific header name: the corresponding HTTP header, such as req.http.Cookie
  • req.hash_always_miss: Force the cache invalidation of this request
  • req.hash_ignore_busy: When lookup cache, ignore busy objects
  • req.can_gzip: set to use gzip
  • req.restarts: set the maximum number of restarts
  • req.esi: Set whether to support ESI, it will change in the future, it is recommended not to use
  • req.esi_level: set the level of ESI
  • req.grace: Set the time the object is held
  • req.xid: the unique id of the request

The following variables are used when accessing the backend server

  • bereq.request: the type of request (such as "GET", "HEAD")
  • bereq.url: requested url
  • bereq.proto: the requested protocol
  • bereq.http. Specific header name: HTTP header of the request, such as bereq.http.Cookie
  • bereq.connect_timeout: the time to wait for the backend server to respond
  • bereq.first_byte_timeout: Waiting time to receive the first byte, invalid in pipe mode
  • bereq.between_bytes_timeout: the interval between two bytes received from the backend server, the pipe mode is invalid

The following variables are used when they are retrieved from the back-end server but have not yet entered the cache, that is, the vcl_fetch variable

  • beresp.do_stream: The object is returned directly to the client and will not be cached in Varnish. These objects will be marked as busy in Varnish3
  • beresp.do_esi: Whether to perform ESI processing
  • beresp.do_gzip: Whether to Gzip compression before storage
  • beresp.do_gunzip: Whether to unzip before storing
  • beresp.http. Specific header name: HTTP header, such as beresp.http.Cookie
  • beresp.proto: HTTP protocol
  • beresp.status: HTTP status code
  • beresp.response: the status message returned by the server
  • beresp.ttl: the time the object was saved
  • beresp.grace: the time the object grace was saved
  • beresp.saintmode: the duration of saint mode
  • beresp.backend.name: the name of the backend of the response
  • beresp.backend.ip:response的backend的ip
  • beresp.backend.port: the port of the backend of the response
  • beresp.storage: Force Varnish to save this object

The following variables are valid after the request target is successfully obtained from the backend server or cache

  • obj.proto: returns the HTTP version of the request target
  • obj.status: HTTP status code returned by the server
  • obj.response: HTTP status information returned by the server
  • obj.ttl: The remaining survival time of the target, in seconds.
  • obj.lastuse: The elapsed time after the last request, in seconds.
  • obj.hits: Approximate number of delivered times. If it is 0, it means that the cache is wrong.
  • obj.grace: the survival time of object grace
  • obj.http. The name of the specific header: Http header

The following variables are valid after the target hash key

  • req.hash: The hashkey is related to the target in the cache and is used when reading and writing to the cache.
  • The following variables are used when preparing to respond to the client
  • resp.proto: The HTTP protocol version for which the response is prepared
  • resp.status: returns the HTTP status code of the client
  • resp.response: returns the HTTP status information of the client
  • resp.http.header: HTTP header in communication
    with the SET keyword, the value to assign to the variable:
sub vcl_recv {
    
    
    # Normalize the Host: header
    if (req.http.host ~ "^(www.)?example.com$") {
        set req.http.host = "www.example.com";
    }
}

You can use the remove keyword to completely delete the HTTP header:

sub vcl_fetch {
    
    
    remove obj.http.Set-Cookie;
}

Common VCL application snippets

1. Set different header parameters for different devices:

sub vcl_recv {
    if (req.http.User-Agent ~ "iPad" ||
        req.http.User-Agent ~ "iPhone" ||
        req.http.User-Agent ~ "Android") {
        set req.http.X-Device = "mobile";
    } else {
        set req.http.X-Device = "desktop";
    }
}

2. Want to cancel the cookie of the request to access /images:

sub vcl_recv {
    
    
    if (req.url ~ "^/images") {
        unset req.http.cookie;
    }
}

3. Control the accessible ip address through ACL

acl local {
    "localhost";
    "192.168.1.0"/24; /* and everyone on the local network */
    ! "192.168.1.23"; /* except for the dialin router */
}
sub vcl_recv {
    
    
    if (req.request == "PURGE") {
        if (client.ip ~ local) {
            return(lookup);
        }
    }
}
sub vcl_hit {
    
    
    if (req.request == "PURGE") {
        set obj.ttl = 0s;
        error 200 "Purged.";
    } 
}
sub vcl_miss {
    
    
    if (req.request == "PURGE") {
        error 404 "Not in cache.";
    }
}

4. Modify the ttl of the object returned from the background server:

sub vcl_fetch {
    
    
    if (req.url ~ "\.(png|gif|jpg)$") {
        unset beresp.http.set-cookie;
        set beresp.ttl = 1h;
    }
}

5. Set the accept-encoding header sent by the client to only have two encodings: gzip and deflate, gzip takes precedence

if (req.http.Accept-Encoding) {
    if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
        # No point in compressing these
        remove req.http.Accept-Encoding;
    } elsif (req.http.Accept-Encoding ~ "gzip") {
        set req.http.Accept-Encoding = "gzip";
    } elsif (req.http.Accept-Encoding ~ "deflate") {
        set req.http.Accept-Encoding = "deflate";
    } else {
        # unknown algorithm
        remove req.http.Accept-Encoding;
    }
}

6. Simple picture anti-theft link:

if (req.http.referer ~ "http://.*") {
    if ( !(req.http.referer ~ "http://.*baidu\.com"
        || req.http.referer ~ "http://.*google\.com"
        || req.http.referer ~ "http://.*google\.cn"
    )) {
        set req.http.host = "www.baidu.com";
        set req.url = "/static/images/logo.gif";
    }
    return (lookup);
}

Guess you like

Origin blog.csdn.net/qq_32198277/article/details/77103632