当前位置: 首页 » 产品 » 新闻资讯 » 正文

Linux下Varnish缓存服务器的安装与配置

放大字体  缩小字体 发布日期: 2024-10-20 13:36   来源:http://www.baidu.com/  作者:无忧资讯  浏览次数:11
核心提示:  Varnish是一款高性能且开源的反向代理服务器和http加速器。与传统的Squid相比,Varnish具有性能更高、速度更快、管理更方便

  Varnish是一款高性能且开源的反向代理服务器和http加速器。与传统的Squid相比,Varnish具有性能更高、速度更快、管理更方便等诸多优点。作者Poul-Henning Kamp是FreeBSD的内核开发者之一。Varnish采用全新的软件体系架构,和现在的硬件提交配合紧密。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算 机系统的内存除了主存外,还包括了cpu内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此squid cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。

  挪威最大的在线报纸 Verdens Gang () 使用3台Varnish代替了原来的12台squid,性能居然比以前更好,这是Varnish最成功的应用案例。

  Varnish特点:

  1.基于内存进行缓存,重启后数据将消失

  2.利用虚拟内存方式,I/O性能好

  3.支持设置0~60秒的精确缓存时间

  4.VCL配置管理比较灵活

  5.32位机器上缓存文件大小最大为2G

  6.具有强大的管理功能,例如top、stat、admin、list等

  7.状态机设置巧妙,结构清晰

  8.利用二叉堆管理缓存文件,可达到积极删除目的

  Varnish与Squid的对比

  Squid是一个高性能的代理缓存服务器,它和varnish之间有诸多的异同点,如下:

  相同点:

  都是一个反向代理服务器

  都是开源软件

  不同点,也是Varnish的优点:

  Varnish的稳定性很高,两者在完成相同负荷的工作时,Squid服务器发生故障的几率要高于Varnish,因为使用Squid要经常重启。

  Varnish访问速度更快,Varnish采用了“Visual Page Cache”技术,所有缓存数据都直接从内存读取,而squid是从硬盘读取,因而Varnish在访问速度方面会更快。

  Varnish可以支持更多的并发连接,因为Varnish的TCP连接释放要比Squid快。因而在高并发连接情况下可以支持更多TCP连接。

  Varnish可以通过管理端口,使用正则表达式批量的清除部分缓存,而Squid是做不到的。

  squid属于是单进程使用单核CPU,但Varnish是通过fork形式打开多进程来做处理,所以是合理的使用所有核来处理相应的请求。

  当然,与传统的Squid相比,Varnish也是有缺点的,如下:

  varnish进程一旦挂起、崩溃或者重启,缓存数据都会从内存中完全释放,此时所有请求都会发送到后端服务器,在高并发情况下,会给后端服务器造成很大压力。

  在varnish使用中如果单个url的请求通过HA/F5(负载均衡)每次请求不同的varnish服务器中,被请求varnish服务器都会被穿透到后端,而且同样的请求会在多台服务器上缓存,也会造成varnish的缓存的资源浪费,也会造成性能下降。

  解决方案:

  综上所述在访问量很大的情况下推荐使用varnish的内存缓存方式启动,而且后面需要跟多台squid服务器。主要为了防止前面的varnish服务、服务器被重启的情况下,前期肯定会有很多的穿透这样squid可以担当第二层cache,而且也弥补了varnish缓存在内存中重启都会释放的问题。

  这样的问题可以在负载均衡上做url哈希,让单个url请求固定请求到一台varnish服务器上,可以解决该问题。

  varnish的工作流程

  1、进程之间通信

  varnish启动或有2个进程 master(management)进程和child(worker)进程。master读入存储配置命令,进行初始化,然后fork,监控child。child则分配线程进行cache工作,child还会做管理线程和生成很多worker线程。

  child进程主线程初始化过程中,将存储大文件整个加载到内存中,如果该文件超出系统的虚拟内存,则会减少原来配置mmap大小,然后继续加载,这时候创建并初始化空闲存储结构体,放在存储管理的struct中,等待分配。

  接着varnish某个负责接口新http连接的线程开始等待用户,如果有新的http连接,但是这个线程只负责接收,然后唤醒等待线程池中的work线程,进行请求处理。

  worker线程读入uri后,将会查找已有的object,命中直接返回,没有命中,则会从后端服务器中取出来,放到缓存中。如果缓存已满,会根据LRU算法,释放旧的object。对于释放缓存,有一个超时线程会检测缓存中所有object的生命周期,如果缓存过期(ttl),则删除,释放相应的存储内存。

  2、配置文件各结构之间通信

2016411132937298.jpg (864×1085)

  Varnish安装

  代码如下:

  wget

  tar xzf pcre-8.33.tar.gz

  cd pcre-8.33

  http://www.jsgho.com/help/fwq/configure

  make && make install

  cd http://www.jsgho.com/help/

  varnish-3.0.4报错如下:

  varnishadm.c:48:33: error: editline/readline.h: No such file or directory

  varnishadm.c: In function 'cli_write':

  varnishadm.c:76: warning: implicit declaration of function 'rl_callback_handler_remove'

  varnishadm.c:76: warning: nested extern declaration of 'rl_callback_handler_remove'

  varnishadm.c: In function 'send_line':

  varnishadm.c:179: warning: implicit declaration of function 'add_history'

  varnishadm.c:179: warning: nested extern declaration of 'add_history'

  varnishadm.c: In function 'varnishadm_completion':

  varnishadm.c:216: warning: implicit declaration of function 'rl_completion_matches'

  varnishadm.c:216: warning: nested extern declaration of 'rl_completion_matches'

  varnishadm.c:216: warning: assignment makes pointer from integer without a cast

  varnishadm.c: In function 'pass':

  varnishadm.c:233: error: 'rl_already_prompted' undeclared (first use in this function)

  varnishadm.c:233: error: (Each undeclared identifier is reported only once

  varnishadm.c:233: error: for each function it appears in.)

  varnishadm.c:235: warning: implicit declaration of function 'rl_callback_handler_install'

  varnishadm.c:235: warning: nested extern declaration of 'rl_callback_handler_install'

  varnishadm.c:239: error: 'rl_attempted_completion_function' undeclared (first use in this function)

  varnishadm.c:300: warning: implicit declaration of function 'rl_forced_update_display'

  varnishadm.c:300: warning: nested extern declaration of 'rl_forced_update_display'

  varnishadm.c:303: warning: implicit declaration of function 'rl_callback_read_char'

  varnishadm.c:303: warning: nested extern declaration of 'rl_callback_read_char'

  make[3]: *** [varnishadm-varnishadm.o] Error 1

  make[3]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin/varnishadm'

  make[2]: *** [all-recursive] Error 1

  make[2]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin'

  make[1]: *** [all-recursive] Error 1

  make[1]: Leaving directory `/root/lnmp/src/varnish-3.0.4'

  make: *** [all] Error 2

  报错没找到解决方法,选varnish-3.0.3

  代码如下:

  wget

  tar xzf varnish-3.0.3.tar.gz

  cd varnish-3.0.3

  export PKG_CONFIG_PATH=http://www.3lian.com/usr/local/lib/pkgconfig

  http://www.jsgho.com/help/fwq/configure --prefix=http://www.3lian.com/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking --with-jemalloc

  make && make install

  /usr/bin/install -m 755 http://www.jsgho.com/help/fwq/redhat/varnish.initrc /etc/init.d/varnish

  /usr/bin/install -m 644 http://www.jsgho.com/help/fwq/redhat/varnish.sysconfig /etc/sysconfig/varnish

  /usr/bin/install -m 755 http://www.jsgho.com/help/fwq/redhat/varnish_reload_vcl /usr/local/varnish/bin

  useradd -M -s /sbin/nologin varnish

  代码如下:

  ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/

  ln -s /usr/local/varnish/bin/varnish_reload_vcl /usr/bin/

  ln -s /usr/local/varnish/bin/varnishadm /usr/bin/

  代码如下:

  chkconfig --add varnish

  chkconfig varnish on

  生成varnish管理秘钥:

  代码如下:

  uuidgen > /usr/local/varnish/etc/varnish/secret

  chmod 644 /usr/local/varnish/etc/varnish/secret

  修改varnish启动配置:

  代码如下:

  sed -i "s@^VARNISH_VCL_CONF=http://www.3lian.com/etc/varnish/default.vcl@#VARNISH_VCL_CONF=http://www.3lian.com/etc/varnish/default.vclnVARNISH_

VCL_CONF=http://www.3lian.com/usr/local/varnish/etc/varnish/linuxeye.vcl@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_LISTEN_PORT=6081@#VARNISH_LISTEN_PORT=6081nVARNISH_LISTEN_PORT=80@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_SECRET_FILE=http://www.3lian.com/etc/varnish/secret@#VARNISH_SECRET_FILE=http://www.3lian.com/etc/varnish/secretnVARNISH_

SECRET_FILE=http://www.3lian.com/usr/local/varnish/etc/varnish/secret@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_STORAGE_FILE=http://www.3lian.com/var/lib/varnish/varnish_storage.bin@#VARNISH_STORAGE_FILE=http://www.3lian.com/var/lib/varnis

h/varnish_storage.bin

nVARNISH_STORAGE_FILE=http://www.3lian.com/usr/local/varnish/var/varnish_storage.bin@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_STORAGE_SIZE.*@VARNISH_STORAGE_SIZE=150M@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_STORAGE=.*@VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"@" /etc/sysconfig/varnish

  假设你的服务器拥有多颗逻辑处理器,还可以做以下的设置:

  /etc/sysconfig/varnish 里面还可以添加自定义的参数,用”-p 参数“的方式添加,如:

  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_TIMEOUT}

  -u varnish -g varnish

  -S ${VARNISH_SECRET_FILE}

  -s ${VARNISH_STORAGE}

  -p thread_pools=2" #这里为添加项

 
 
[ 产品搜索 ]  [ 加入收藏 ]  [ 告诉好友 ]  [ 打印本文 ]  [ 违规举报 ]  [ 关闭窗口 ]

 

 
推荐图文
推荐产品
点击排行
    行业协会  备案信息  可信网站