Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1760623
  • 博文数量: 234
  • 博客积分: 4966
  • 博客等级: 上校
  • 技术积分: 3322
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-13 01:03
文章分类

全部博文(234)

文章存档

2017年(2)

2016年(1)

2015年(8)

2014年(11)

2013年(44)

2012年(27)

2011年(22)

2010年(30)

2009年(37)

2008年(6)

2007年(45)

2006年(1)

分类: LINUX

2014-05-28 21:12:15

    具体怎么安装就不多说了,具体可参考我的这篇博文《 怎么样在CentOS/Redhat中安装Varnish 3

Varnish3.x的配置文件
/etc/varnish/ 存放varnish VCL配置文件
/etc/sysconfig/varnish 【CentOS】 存放varnish服务器运行的参数
/usr/sbin/varnishd varnish服务器执行文件
/etc/init.d/varnish 运行程序

首先介绍一下/etc/sysconfig/varnish配置文件的参数说明(我自己的理解,如有错误欢迎大家指正)
[edward@localhost ~]$ vim /etc/sysconfig/varnish
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#

# Maximum number of open files (for ulimit -n)
NFILES=131072        #打开文件数目的限制的默认值

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000        #应该是共享内存的默认值吧

# Maximum number of threads (for ulimit -u)
NPROCS="unlimited"        #最大线程数的默认值

# Maximum size of corefile (for ulimit -c). Default in Fedora is 0
# DAEMON_COREFILE_LIMIT="unlimited"

# Set this to 1 to make init script reload try to switch vcl without restart.
# To make this work, you need to set the following variables
# explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS,
# VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE, or in short,
# use Alternative 3, Advanced configuration, below
RELOAD_VCL=1        #选择VCL配置文件的模式

# This file contains 4 alternatives, please use only one.

## Alternative 1, Minimal configuration, no VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# content server on localhost:8080.  Use a fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -b localhost:8080 \
#             -u varnish -g varnish \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"


## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a
# fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -f /etc/varnish/default.vcl \
#             -u varnish -g varnish \
#             -S /etc/varnish/secret \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"


## Alternative 3, Advanced configuration
#
# See varnishd(1) for more information.
#
# # 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=127.0.0.1        #varnish的监听地址
VARNISH_LISTEN_PORT=6081
                #varnish的监听端口
#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1        #varnish管理工具访问的监听地址
VARNISH_ADMIN_LISTEN_PORT=6082
                #varnish管理工具访问的监听端口
#
# # Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret        #varnish管理工具所需的共享密钥
#
# # The minimum number of worker threads to start
VARNISH_MIN_THREADS=50            #varnish启动时开启的最小线程数默认值
#
# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000            #最大能达到的线程数默认值
#
# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120        #好像是一个线程空闲120s就自动回收吧
#
# # Cache file location
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin        #varnish缓存文件的路径
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=2G            #varnish缓存的大小,默认值是1G
#
# # Backend storage specification
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"        #varnish采用系统文件方式缓存
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
                               #varnish直接使用内存方式缓存
#varnish缓存有三种方式,file,malloc,persistent
#file就是先创建一个系统文件,然后再用二分法分段方式映射到内存中缓存数据(二分法分段方式是啥我也不懂,网上看的,另外应该是先存数据到系统文件中,再映射到内存中,一般是设备内存较小或者VPS之类的才用这种方式,这个对系统IO有较大影响),另外网上是说file方式有个缺点是缓存系统文件不会自动清除,要手工清除。
#土豪,内存较多的系统肯定是用malloc方式了,这个是直接在内存中开辟一块空间给varnish缓存数据,优点是速度快,但缺点是服务停止或者系统挂了重启服务时,内存中的缓存容易丢失,建议在varnish后面再加个squid集群,避免对后端造成冲击
#persistent方式,测试阶段,跟file方式类似,好像是说优点介于file和malloc之间,另外没有在重启后残留缓存系统文件的缺点了

#
# # Default TTL used when the backend does not specify one
VARNISH_TTL=120
#
# # DAEMON_OPTS is used by the init script.  If you add or remove options, make
# # sure you update this section, too.
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
             -f ${VARNISH_VCL_CONF} \
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
             -t ${VARNISH_TTL} \
             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_T                                                                                        IMEOUT} \
             -u varnish -g varnish \
             -S ${VARNISH_SECRET_FILE} \
             -s ${VARNISH_STORAGE}"

#


## Alternative 4, Do It Yourself. See varnishd(1) for more information.
#
# DAEMON_OPTS=""

接下来是修改/etc/varnish/default.vcl规则文件了(说实话,这个也不大怎么会配置,在网上找的一些配置,但还能够生效)
#后端配置
backend tomcat {
        .host = "127.0.0.1";
        .port = "8080";
        .connect_timeout = 60s;
        .first_byte_timeout = 10s;
        .between_bytes_timeout = 10s;
}
backend tomcat1 {
        .host = "127.0.0.1";
        .port = "8081";
        .connect_timeout = 60s;
        .first_byte_timeout = 10s;
        .between_bytes_timeout = 10s;
}
backend tomcat2 {
        .host = "127.0.0.1";
        .port = "8082";
        .connect_timeout = 60s;
        .first_byte_timeout = 10s;
        .between_bytes_timeout = 10s;
}
#集群
director tomcatweb round-robin {
        {
        .backend = tomcat;
        }
        {
        .backend = tomcat1;
        }
        {
        .backend = tomcat2;
        }
}
acl purge {
        "localhost";
        "127.0.0.1";
}
sub vcl_recv
{
        set req.backend = tomcatweb;
        if (req.request == "PURGE")
        {
                if (!client.ip ~ purge)
                {
                error 405 "Not allowed.";
                }
                return (lookup);
        }
        if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-                                                                                        9]+)?$")
        {
                unset req.http.Cookie;
        }
        if (req.request != "GET" && req.request != "HEAD")
        {
                return (pass);
        }
        if (req.http.Authorization || req.http.Cookie)
        {
                return (pass);
        }
        return (lookup);
}
sub vcl_miss
{
        if (req.request == "PURGE")
        {
                error 404 "Not in cache.";
        }
}
sub vcl_fetch
{
        if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)(\?[a-z0-                                                                                        9]+)?$")
        {
                unset beresp.http.set-cookie;
        }
        set beresp.grace = 6h;
}
sub vcl_hash
{
        hash_data(req.url);
        if (req.http.host)
        {
                hash_data(req.http.host);
        }
        else
        {
                hash_data(server.ip);
        }
        return (hash);
}
sub vcl_hit
{
        if (req.request == "PURGE")
        {
                purge;
                error 200 "Purged.";
        }
}

sub vcl_miss
{
        if (req.request == "PURGE")
        {
                purge;
                error 200 "Purged.";
        }
}

参考:

下面两个图是vcl的访问流程图,很有意义




阅读(2646) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~