---
title: Varnish调优
description: "通过调优Varnish的配置文件，可以有效地提高服务器的性能。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengwebs/ecosystemEnable/Varnish/kunpengvarnish_05_0013.html
sourcePath: /source/zh/kunpengwebs/ecosystemEnable/Varnish/kunpengvarnish_05_0013.html
indexId: b9d7761a05bf4f6ef561da9f4c46190d40c85c94ab96aefbaec9ff68bfbb3c9474
---
# Varnish调优

#### 目的

通过调优Varnish的配置文件，可以有效地提高服务器的性能。


#### 方法

1. 配置Varnish的配置文件default.vcl中的default，vcl_recv，vcl_backend_response，vcl_pipe，vcl_pass，vcl_hash，vcl_hit，vcl_miss，vcl_deliver模块。文件路径：“/usr/local/varnish/config/default.vcl”。配置示例见default.vcl配置文件示例。配置项说明如表 Varnish配置项说明所示。
**表1 Varnish配置项说明**

| 配置项 | 说明 |
| --- | --- |
| default | 配置通信IP及端口。 |
| vcl\_recv | 用于接收和处理请求；当请求到达Varnish，通过判断请求的数据来决定如何处理请求。 |
| vcl\_backend\_response | 获得后端主机的响应后，调用。 |
| vcl\_pipe | 用于将请求直接传递至后端主机，并将后端响应原封不动返回给客户端。 |
| vcl\_pass | 用于将请求直接传递给后端主机，但后端主机的响应并不缓存，而是直接返回给客户端。 |
| vcl\_hash | 在vcl\_recv调用后为请求创建一个hash值时，调用。此hash值将作为Varnish中hash表的key。 |
| vcl\_hit | 在缓存中找到请求的内容后自动调用。 |
| vcl\_miss | 在缓存中没有找到请求的内容后自动调用。用于判断是否需要从后端服务器获取内容。 |
| vcl\_deliver | 将在缓存中找到的请求的内容发送给客户端前调用。 |


2. 对Varnish进程进行绑核并启动。
  以物理机1P场景为例，使用numactl设置CPU与内存的亲和性。命令参数说明参考表 命令参数说明。

  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

**表2 命令参数说明**

| 参数名称 | 参数说明 |
| --- | --- |
| \-C | 进程绑定CPU。 |
| \-\-membind | 分配进程的内存，其中0，1表示分配的内存在node0和node1上。 |
| \-P | Varnish进程pid文件存放路径。 |
| \-a | 表示Varnish对httpd的监测地址及端口。 |
| \-T | 设定Varnish的telnet管理地址及其端口。 |
| \-s | 指定Varnish缓存存放的方式，常用的方式有：“\-s file,<dir\_or\_file>,<size>”。 |
| \-f | 指定Varnish的配置文件位置。 |
| \-t | 指定默认的TTL值。 |
| \-p thread\_pool\_max | 每个线程池创建的最大线程数。 |
| \-p thread\_pools | 设置线程池个数。 |
| \-p thread\_pool\_destroy\_delay | 摧毁线程的延迟时间，也就是摧毁之前需要犹豫一下的时间。 |
| \-p timeout\_idle | 设置保持链接的空闲时长。 |
| \-p thread\_pool\_min | 每个线程池创建的最小线程数。 |


#### default.vcl配置文件示例

```
# 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, access "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";
        }
}
```
