varnish的简单配置

虽然使用nginx和http的时候都可以基于cache模块使用缓存功能,但当用户的并发上升到一定等级时,web服务自带的缓存功能是远远不够的,这时我们就需要一台专门管理缓存的服务器。varnish是一款开源的缓存服务软件,相对于squid更加的轻量级。

varnish的程序环境:
/etc/varnish/varnish.params: 配置varnish服务进程的工作特性,例如监听
的地址和端口,缓存机制;
/etc/varnish/default.vcl:配置各Child/Cache线程的缓存策略;
主程序:
    /usr/sbin/varnishd
CLI interface:
    /usr/bin/varnishadm
VCL配置文件重载程序:
    /usr/sbin/varnish_reload_vcl
varnish的缓存存储机制( Storage Types):
  -s [name=]type[,options]  
    · malloc[,size]
        内存存储,[,size]用于定义空间大小;重启后所有缓存项失效;
    · file[,path[,size[,granularity]]]
        磁盘文件存储,黑盒;重启后所有缓存项失效;
    · persistent,path,size
        文件存储,黑盒;重启后所有缓存项有效;实验;

通过yum安装完varnish后,首先我们来看看varnish.params配置文件

# Varnish environment configuration description. This was derived from
# the old style sysconfig/defaults settings

# Set this to 1 to make systemd reload try to switch VCL without restart.
RELOAD_VCL=1

# Main configuration file. You probably want to change it.
VARNISH_VCL_CONF=/etc/varnish/default.vcl  #指定默认的vcl文件

# Default address and port to bind to. Blank address means all IPv4
# and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
# quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=192.168.1.5
VARNISH_LISTEN_PORT=80 #指定监听的端口

# Admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 #管理地址
VARNISH_ADMIN_LISTEN_PORT=6082

# Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret #口令文件

# Backend storage specification, see Storage Types in the varnishd(5)
# man page for details.
VARNISH_STORAGE="file,/var/varnish/cache,256M" #这个是我们主要改的,存储的位置,方法形式

# User and group for the varnishd worker processes
VARNISH_USER=varnish
VARNISH_GROUP=varnish

# Other options, see the man page varnishd(1)
#DAEMON_OPTS="-p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300"    
varnish程序的选项:
            程序选项:/etc/varnish/varnish.params文件
                -a address[:port][,address[:port][...],默认为6081端口; 
                -T address[:port],默认为6082端口;
                -s [name=]type[,options],定义缓存存储机制;
                -u user
                -g group
                -f config:VCL配置文件;
                -F:运行于前台;

然后我们使用varnishadm来登陆接口
varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
登陆进来使用help查看可用的命令

    
            配置文件相关:
                vcl.list 
                vcl.load:装载,加载并编译;
                vcl.use:激活;
                vcl.discard:删除;
                vcl.show [-v] :查看指定的配置文件的详细信息;
                
            运行时参数:
                param.show -l:显示列表;
                param.show 
                param.set  
                
            缓存存储:
                storage.list
                
            后端服务器:
                backend.list 

下面我们来介绍下VCL配置文件,里面的功能是通过一种语言实现的。VCL有多个状态引擎,状态之间存在相关性,但状态引擎彼此间互相隔离;每个状态引擎可使用return(x)指明关联至哪个下一级引擎;每个状态引擎对应于vcl文件中的一个配置段,即为subroutine。


varnish的简单配置_第1张图片
image.png
vcl_recv的默认配置:
            
                sub vcl_recv {
                    if (req.method == "PRI") {
                        /* We do not support SPDY or HTTP/2.0 */
                        return (synth(405));
                    }
                    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 (req.method != "GET" && req.method != "HEAD") {
                        /* We only deal with GET and HEAD by default */
                        return (pass);
                    }
                    if (req.http.Authorization || req.http.Cookie) {
                        /* Not cacheable by default */
                        return (pass);
                    }
                        return (hash);
                    }
                }

里面还有许许多多的变量

变量类型:
            内建变量:
                req.*:request,表示由客户端发来的请求报文相关;
                    req.http.*
                        req.http.User-Agent, req.http.Referer, ...
                bereq.*:由varnish发往BE主机的httpd请求相关;
                    bereq.http.*
                beresp.*:由BE主机响应给varnish的响应报文相关;
                    beresp.http.*
                resp.*:由varnish响应给client相关;
                obj.*:存储在缓存空间中的缓存对象的属性;只读;
                
                常用变量:
                    bereq.*, req.*:
                        bereq.http.HEADERS
                        bereq.request:请求方法;
                        bereq.url:请求的url;
                        bereq.proto:请求的协议版本;
                        bereq.backend:指明要调用的后端主机;
                        
                        req.http.Cookie:客户端的请求报文中Cookie首部的值; 
                        req.http.User-Agent ~ "chrome"
                        
                        
                    beresp.*, resp.*:
                        beresp.http.HEADERS
                        beresp.status:响应的状态码;
                        reresp.proto:协议版本;
                        beresp.backend.name:BE主机的主机名;
                        beresp.ttl:BE主机响应的内容的余下的可缓存时长;
                        
                    obj.*
                        obj.hits:此对象从缓存中命中的次数;
                        obj.ttl:对象的ttl值
                        
                    server.*
                        server.ip
                        server.hostname
                    client.*
                        client.ip                   
                
            用户自定义:
                set 
                unset 

默认的VCL文件

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "192.168.31.201";
    .port = "80";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do acc

你可能感兴趣的:(varnish的简单配置)