Rate This Document
Findability
Accuracy
Completeness
Readability

Varnish Tuning

Purpose

Modify the Varnish configuration file to improve the server performance.

Procedure

  1. Configure the default, vcl_recv, vcl_backend_response, vcl_pipe, vcl_pass, vcl_hash, vcl_hit, vcl_miss, and vcl_deliver modules in the Varnish configuration file default.vcl. The configuration file path is /usr/local/varnish/config/default.vcl. For details about the configuration example, see Example of the default.vcl configuration file. See Table Varnish Configuration Item Description for the configuration item description.
    Table 1 Varnish Configuration Item Description

    Configuration Item

    Description

    default

    Configures the communication IP address and port.

    vcl_recv

    Receives and processes requests. When a request reaches Varnish, Varnish determines how to process the request based on the request data.

    vcl_backend_response

    Calls the command after receiving the response from the backend host.

    vcl_pipe

    Directly sends requests to backend hosts and return backend responses to clients without any change.

    vcl_pass

    Directly sends requests to backend hosts. Responses from backend hosts are not cached, but directly returned to clients.

    vcl_hash

    Calls the command after vcl_recv is called to create a hash value for the request. The hash value is used as the key of the hash table in Varnish.

    vcl_hit

    Automatically calls the command after the requested content is found in the cache.

    vcl_miss

    Automatically calls the command when the requested content is not found in the cache. Determines whether to obtain content from the backend server.

    vcl_deliver

    Calls the command before the requested content found in the cache is sent to the client.

  2. Bind cores to the Varnish process and start the process.

    Uses the physical machine 1P as an example. Runs the numactl command to set the affinity between the CPU and memory. For details about the command parameters, see Table Command Parameter Description.

    1
    numactl -C 0-2,4-10,12-18,20-26,28-34,36-42,44-47 --membind=0,1 /usr/local/varnish/sbin/varnishd -P /tmp/varnish.pid -a :12345 -T 127.0.0.1:6082 -s malloc,100GB -f /usr/local/varnish/config/default.vcl -t 600 -p thread_pool_max=5000 -p thread_pools=10 -p thread_pool_destroy_delay=3 -p timeout_idle=75 -p thread_pool_min=700
    
    Table 2 Command parameter description

    Parameter

    Description

    -C

    Binds a process to CPU.

    --membind

    Specifies the memory allocated to a process. 0 and 1 indicate that the allocated memory is on node 0 and node 1.

    -P

    Specifies the path for storing the PID file of the Varnish process.

    -a

    Specifies the listening IP address and port number of Varnish on the httpd.

    -T

    Specifies the Telnet management address and port number of Varnish.

    -s

    Specifies the Varnish cache storage mode. The common modes include -s file, <dir_or_file>, and <size>.

    -f

    Specifies the location of the Varnish configuration file.

    -t

    Specifies the default TTL value.

    -p thread_pool_max

    Specifies the maximum number of threads created in each thread pool.

    -p thread_pools

    Specifies the number of thread pools.

    -p thread_pool_destroy_delay

    Specifies the delay time for destroying a thread, that is, the hesitation time before destroying a thread.

    -p timeout_idle

    Specifies the idle duration for keeping a connection

    -p thread_pool_min

    Specifies the minimum number of threads created in each thread pool.

Example of the default.vcl configuration file

# This is an example VCL file for Varnish.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
import directors;
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "xxx.xxx.xx.xx";
    .port = "80";
}

sub vcl_recv {
        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;
        }
        ##if request type is not GET, HEAD, PUT, POST, TRACE, OPTIONS, DELETE, acess "pipe" mode
        if (req.method != "GET" &&
            req.method != "HEAD" &&
                req.method != "PUT" &&
                req.method != "POST" &&
                req.method != "TRACE" &&
                req.method != "OPTIONS" &&
                req.method != "DELETE") {
                /* Non-RFC2616 or CONNECT which is weird. */
                return (pipe);
        }
        ##if request type is not GET or HEAD, it will access pass mode
        if (req.method != "GET" && req.method != "HEAD") {
            return (pass);
    }
        if (req.http.Authorization || req.http.Cookie) {
            return (pass);
        }
}

sub vcl_backend_response {
        set beresp.ttl = 120m;
}

sub vcl_pipe {
     return (pipe);
}

sub vcl_pass {
     if (req.method == "PURGE"){
                return(synth(502,"PURGE on a passed object"));
         }
}

sub vcl_hash {
  hash_data(req.url);
  if (req.http.host) {
    hash_data(req.http.host);
  } else {
    hash_data(server.ip);
  }
  return (lookup);
}

sub vcl_hit {
  return (deliver);
}

sub vcl_miss {
  return (fetch);
}

sub vcl_deliver { 
        set req.http.Connection = "keep-alive";
        if (obj.hits > 0){
            set resp.http.X-Cache = "HIT from" + server.ip;
        } else{
           set resp.http.X-Cache = "MISS";
        }
}