varnish 参数说明

vcl_init

Called when VCL is loaded, before any requests pass through it. Typically used to initialize VMODs.

当VCL加载时调用,之后加载客户请求。一般用于初始化VMOD模块。

return() values:

返回值有:

ok Normal return, VCL continues loading.

OK 正常返回值,返回OK后VCL加载。

vcl_recv

Called at the beginning of a request, after the complete request has been received and parsed. 

Its purpose is to decide whether or not to serve the request, how to do it, and, if applicable, 

which backend to use.

在请求完全接收并解析完之后,从请求的开始处调用,他的目的是决定是否处理请求,怎么处理请求,或者如果后端有多个服务器的话,决定去调用哪个。

The vcl_recv subroutine may terminate with calling return() on one of the following keywords:

vcl_recv子程序可以通过调用以下关键字通过return()来终止处理:

error code [reason]Return the specified error code to the client and abandon the request.

error code [reason]  给客户端返回指定的错误代码并丢弃请求。

pass  Switch to pass mode. Control will eventually pass to vcl_pass.

pass  传送到pass模式,控制权将最终交给vcl_pass.

pipe   Switch to pipe mode. Control will eventually pass to vcl_pipe.

pipe  传送到pipe模式,控制权将最终交给vcl_pipe.

lookup   Look up the requested object in the cache. Control will eventually pass to vcl_hit or vcl_miss, 

depending on whether the object is in the cache.

lookup  从缓存中查找对象,控制权将最终交给vcl_hit或者vcl_miss,这取决于是否从缓存中命中对象。

vcl_pipe

Called upon entering pipe mode. In this mode, the request is passed on to the backend, and any further 

data from either client or backend is passed on unaltered until either end closes the connection.

当进入pipe模式时调用,在这个模式下,请求被送到后端服务器,之后的的所有客户端与服务器端的请求将一直以同样的方式传输,直到连接关闭。

The vcl_pipe subroutine may terminate with calling return() with one of the following keywords:

vcl_pipe子程序可以通过调用以下关键字通过return()来终止处理:

error code [reason]  Return the specified error code to the client and abandon the request.

pipe Proceed with pipe mode.

vcl_pass

Called upon entering pass mode. In this mode, the request is passed on to the backend, and the

 backend's response is passed on to the client, but is not entered into the cache. Subsequent requests sub‐ mitted over the same client connection are handled normally.

进入pass模式时调用,在这个模式下,请求被直接送到后端,后端返回的数据被直接返回到客户端,并且不加入缓存中,之后的请求将以同样的方式加以处理。

The vcl_pass subroutine may terminate with calling return() with one of the following keywords:

vcl_piass子程序可以通过调用以下关键字通过return()来终止处理:

error code [reason]  Return the specified error code to the client and abandon the request.

pass  Proceed with pass mode.

restartRestart the transaction. Increases the restart counter. If the number of restarts is higher than 

max_restarts varnish emits a guru meditation error.

restart 重新开始事务。增加重启计数器,如果计数器的值大于max_restarts指定的值,varnish发出一个严重错误。

vcl_hash

You may call hash_data() on the data you would like to add to the hash.

你可以在你想加入hash的数据中去调用hash_data().

The vcl_hash subroutine may terminate with calling return() with one of the following keywords:

vcl_hash子程序可以通过调用以下关键字通过return()来终止处理:

hash Proceed.

hash 开始hash数据。

vcl_hit

Called after a cache lookup if the requested document was found in the cache.

如果数据从缓存中找到则调用vcl_hit.

The vcl_hit subroutine may terminate with calling return() with one of the following keywords:

vcl_hit子程序可以通过调用以下关键字通过return()来终止处理:

deliver  Deliver the cached object to the client. Control will eventually pass to vcl_deliver.

deliver 把缓存命中的数据传送给客户端 ,控制权将最终交给vcl_deliver.

error code [reason]   Return the specified error code to the client and abandon the request.

pass   Switch to pass mode. Control will eventually pass to vcl_pass.

restart  Restart the transaction. Increases the restart counter. If the number of restarts is higher than 

max_restarts varnish emits a guru meditation error.

vcl_miss

Called after a cache lookup if the requested document was not found in the cache. Its purpose is to 

decide whether or not to attempt to retrieve the document from the backend, and which backend to use.

如果数据没有从缓存中找到则调用vcl_miss.它的目的是决定是否尝试从后端服务器检索数据,如果有多个后端服务器的话,决定调用哪个后端服务器。

The vcl_miss subroutine may terminate with calling return() with one of the following keywords:

vcl_miss子程序可以通过调用以下关键字通过return()来终止处理:

error code [reason]  Return the specified error code to the client and abandon the request.

pass   Switch to pass mode. Control will eventually pass to vcl_pass.

fetch  Retrieve the requested object from the backend. Control will eventually pass to vcl_fetch.

fetch 从后端服务器检索数据,控制权将最终交给vcl_fetch.

vcl_fetch

Called after a document has been successfully retrieved from the backend.

当数据成功的从后端服务器检索到之后调用。

The vcl_fetch subroutine may terminate with calling return() with one of the following keywords:

vcl_fetch子程序可以通过调用以下关键字通过return()来终止处理:

deliver  Possibly insert the object into the cache, then deliver it to the client. Control will eventually 

pass to vcl_deliver.

deliver 尽可能的把数据加入缓存,然后传输给客户端 ,控制权将最终交给vcl_deliver.

error code [reason]    Return the specified error code to the client and abandon the request.

hit_for_pass  Pass in fetch. This will create a hit_for_pass object. Note that the TTL for the hit_for_pass 

object will be set to what the current value of beresp.ttl. Control will be handled to vcl_deliver on the 

current request, but subsequent requests will go directly to vcl_pass based on the hit_for_pass object.

hit_for_pass 进入fetch.它将创建一个hit_for_pass对象,hit_for_pass对象的TTL值将会被设置成beresp.ttl的当前值。该请求的控制权将交给vcl_deliver来处理,之后请求将直接交给vcl_pass,取决于hit_for_pass对象。

restart  Restart the transaction. Increases the restart counter. If the number of restarts is higher 

than max_restarts varnish emits a guru meditation error.

vcl_deliver

Called before a cached object is delivered to the client.

在数据传输到客户端之前被调用。

The vcl_deliver subroutine may terminate with one of the following keywords:

vcl_deliver子程序可以通过调用以下关键字通过return()来终止处理:

deliver  Deliver the object to the client.

deliver 把对象传输到客户端。

error code [reason]   Return the specified error code to the client and abandon the request.

restart   Restart the transaction. Increases the restart counter. If the number of restarts is higher 

than max_restarts varnish emits a guru meditation error.

vcl_error

Called when we hit an error, either explicitly or implicitly due to backend or internal errors.

The vcl_error subroutine may terminate by calling return with one of the following keywords:

vcl_error子程序可以通过调用以下关键字通过return()来终止处理:

deliver  Deliver the error object to the client.

restart   Restart the transaction. Increases the restart counter. If the number of restarts is higher than 

max_restarts varnish emits a guru meditation error.

vcl_fini

Called when VCL is discarded only after all requests have exited the VCL. Typically used to clean up VMODs.

return() values:

ok Normal return, VCL will be discarded.

------------------------------------------

启动脚本
#!/bin/bash
### BEGIN INIT INFO
# Author: JeremyWei
# Date: 2011.8.27
# Description: Varnish management
### END INIT INFO

prefix=/usr/local/varnish
varnish_bin=${prefix}/sbin/varnishd
storage=2000M
varnish_opts="-f ${prefix}/etc/varnish/default.vcl -T 127.0.0.1:2000 -a 0.0.0.0:80 -s file,/tmp,${storage}"

case "$1" in
    start)
        echo -n "Starting varnish......"

        $varnish_bin $varnish_opts

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Shutting down Varnish......"

        pkill varnishd

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        $0 start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

Subroutine列表
•vcl_recv
在请求开始时候被调用,在请求已经被接收到并且解析后调用。目的就是决定是否处理这个请求,怎么处理,使用哪个后端。vcl_recv以return结束,参数可以为如下关键字:
error code [reason]:返回错误码给客户端,丢弃请求。
pass:转换到pass模式。控制权最后会转移到vcl_pass。
pipe:转换到pipe模式。控制权最后会转移到vcl_pipe。
lookup:在缓存中寻找请求对象。控制权最后会转移到vcl_hit或者vcl_miss,决定于对象是否在缓存中。
•vcl_pipe
当进入pipe模式的时候被调用。在这个模式中,请求会被转移到后端,后续的数据不管是从客户端还是后端来的都会以不变的方式传送,直到连接关闭为止。vcl_pipe以return结束,参数可以为如下关键字:
error code [reason]:返回错误码给客户端,丢弃请求。
pipe:以pipe模式执行。
•vcl_pass
当进入pass模式的时候会被调用。在这个模式中,请求会被传送到后端,然后后端的响应会被传送回客户端,但是响应不会进入缓存中。接下来通过相同客户端连接发起的请求会以普通的方式来处理。vcl_pass以return结束,参数可以为如下关键字:
error code [reason]:返回错误码给客户端,丢弃请求。
pass:以pass模式执行。
restart:重新启动这个事务。增加了重启计数。如果重启的次数高于max_restarts,varnish会引起一个错误。
•vcl_hash
你如果把想把数据加入到hash中,那么调用hash_data()。vcl_hash以return结束,参数可以为如下关键字:
hash:执行hash逻辑。
•vcl_hit
如果请求的对象在缓存中被找到了,那么在缓存查找结束后被调用。vcl_hit以return结束,参数可以为如下关键字:
deliver:deliver缓存对象到客户端。控制权最后会转移到vcl_deliver。
error code [reason]:返回错误码给客户端,丢弃请求。
pass:切换到pass模式。控制权最后会转移到vcl_pass。
restart:重新启动这个事务。增加了重启计数。如果重启的次数高于max_restarts,varnish会引起一个错误。
•vcl_miss
如果请求的对象在缓存中没有被找到,那么在缓存查找结束后被调用。目的是为了决定是否去后端获取这个请求对象,并且要选择哪个后端。vcl_miss以return结束,参数可以为如下关键字:
error code [reason]:返回错误码给客户端,丢弃请求。
pass:切换到pass模式。控制权最后会转移到vcl_pass。
fetch:去后端获取请求对象。控制权最后会转移到vcl_fetch。
•vcl_fetch
当一个对象被成功从后端获取的时候此方法会被调用。vcl_fetch以return结束,参数可以为如下关键字:
deliver:可能把对象放入缓存中,然后再deliver到客户端。控制权最后会转移到vcl_deliver。
error code [reason]:返回错误码给客户端,丢弃请求。
esi:以ESI形式来处理刚刚被获取到的对象。
pass:切换到pass模式。控制权最后会转移到vcl_pass。
restart:重新启动这个事务。增加了重启计数。如果重启的次数高于max_restarts,varnish会引起一个错误。
•vcl_deliver
当一个缓存的对象被deliver到客户端的时候,此方法会被调用。vcl_deliver以return结束,参数可以为如下关键字:
deliver:发送对象到客户端。
error code [reason]:返回错误码给客户端,丢弃请求。
restart:重新启动这个事务,增加重启计数。如果重启的次数高于max_restarts,varnish会引起一个错误。
•vcl_error
当遇见一个错误的时候会被调用,错误可能是跟后端有关系或者内部错误。vcl_error以return结束,参数可以为如下关键字:
deliver:发送对象到客户端。
restart:重新启动这个事务,增加重启计数。如果重启的次数高于max_restarts,varnish会引起一个错误。

重要变量

subroutine不带参数,一般通过全局变量来实现信息的传递。

如下变量在backend中有效:
•.host:backend的主机名或者IP。
•.port:backend的端口。

如下变量在处理一个请求(例如vcl_recv)的时候可用:
•client.ip:客户端IP地址。
•server.hostname:服务器的主机名。
•server.identity:服务器标示,当启动varnish的时候用”-i”参数来指定。如果varnish启动时候没有指定”-i”参数,那么server.identity会被设置为用”-n”参数所指定的实例名称。
•server.ip:服务器IP地址。
•server.port:服务器端口。
•req.request:请求类型(例如“GET”,“HEAD”)。
•req.url:请求的URL。
•req.proto:HTTP协议版本。
•req.backend:处理请求的后端服务器。
•req.backend.healthy:后端是否健康。health check需要在backend的probe中进行设置。
•req.http.header:相关的HTTP头。
•req.hash_always_miss:强迫对于本次请求的缓存查找结果为miss。如果设置为”true”,那么varnish将会忽略任何存在的缓存对象,一直从后端重新获取资源。
•req.hash_ignore_busy:在缓存查找时候忽略任何忙的对象。如果有两个服务器,彼此互相查找缓存内容,那么可以使用这个变量来避免潜在的死锁。

如下变量在准备一个后端请求(比如在cache miss或者pass,pipe模式)的时候可用:
•bereq.request:请求的类型(比如“GET”,“HEAD”)。
•bereq.url:请求的URL。
•bereq.proto:与后端服务器交互的HTTP协议版本。
•bereq.http.header:相关的HTTP头。
•bereq.connect_timeout:与后端连接的超时时间。
•bereq.first_byte_timeout:从后端返回第一个字节所需等待的秒数,在pipe模式中不可用。
•bereq.between_bytes_timeout:从后端返回的每个字节之间的时间间隔,以秒计。在pipe模式中不可用。

如下的变量在请求对象从后端返回之后,在其被放入缓存之前可用。换句话说,也就是在vcl_fetch中可用。
•beresp.proto:HTTP协议版本。
•beresp.status:后端返回的HTTP状态码(例如200,302等)。
•beresp.response:后端返回的状态内容(例如“OK”,“Found”)。
•beresp.cacheable:如果请求的结果是可以被缓存的,那么此变量为”true”。如果HTTP状态码为200, 203, 300, 301, 302, 404,410之一并且pass没有在vcl_recv中被调用,那么这个结果就是可以被缓存的。如果response的TTL和grace time都为0,那么beresp.cacheable将会为0。beresp.cacheable是可写的。
•beresp.ttl:缓存对象的生存时间,以秒为单位,这个变量是可写的。

在对象已经存在于缓存中并被查询到的时候,一般在vcl_hit和vcl_deliver中,如下的变量(大部分是read-only)可用:
•obj.proto:与后端交互的HTTP版本协议。
•obj.status:后端返回的HTTP状态码。
•obj.response:后端返回的HTTP状态内容。
•obj.cacheable:如果对象的beresp.cacheable为”true”,那么此变量的值为”true”。除非你强制delivery,否则obj.cacheable一直为”true”。
•obj.ttl:缓存对象的生存时间,以秒为单位,这个变量是可写的。
•obj.lastuse:从现在到对象最近一次访问所间隔的时间,以秒为单位。
•obj.hits:对象被发送到客户端的次数,0表示缓存查询miss了。

如下变量在决定对象hash key的时候可用:
•req.hash:hash key被用来关联一个缓存中的对象。在读写缓存的时候都会被用到。

如下变量在准备把一个响应发送给客户端时候可用:
•resp.proto:响应使用的HTTP协议版本。
•resp.status:将要返回的HTTP状态码。
•resp.response:将要返回的HTTP状态内容。
•resp.http.header:相关的HTTP头。

此条目发表在web server分类目录,贴了标签。将固定链接加入收藏夹。