2013-11-10 15:44:01
来 源
ITJS.CN
Varnish
本文介绍Varnish服务器的管理及配置,希望对于Varnish入门者给与帮助。更多Varnish资源请本站内搜索。

一、varnishd指令

Varnish启动的命令是/usr/local/varnish/sbin/varnishd,此命令参数较多,用法比较复杂,在命令行执行“/usr/local/varnish/sbin/varnishd –h”即可得到varnishd的详细用法,表2-6列出了varnishd常用参数的使用方法和含义。

命令参数 参数含义

-a address:port 表示varnish对httpd的监听地址及其端口

-b address:port 表示后端服务器地址及其端口

-d 表示使用debug调试模式

-f file 指定varnish服务器的配置文件

-p param=value 指定服务器参数,用来优化varnish性能

-P file Varnish进程PID文件存放路径

-n dir 指定varnish的工作目录

-s kind[,storageoptions] 指定varnish缓存内容的存放方式,常用的方式有:“-s file,<dir_or_file>,<size>”。

其中“<dir_or_file>”指定缓存文件的存放路径,“<size>”指定缓存文件的大小

-t 指定缺省的TTL值

-T address:port 设定varnish的telnet管理地址及其端口

-w int[,int[,int]] 设定varnish的工作线程数,常用的方式有:

-w min,max

-w min,max,timeout

例如:-w5,51200,30,这里需要说明下,在varnish2.0版本以后,最小启动的线程数不能设定过大,设置过大,会导致varnish运行异常缓慢。

-V 显示varnish版本号和版权信息

二、 配置varnish运行脚本

在安装varnish一节中,已经将varnish的管理脚本拷贝到了相应的目录下,这里稍作修改即可使用,首先修改/etc/sysconfig/varnish文件,根据本章的实例,配置好的文件如下:

NFILES=131072

MEMLOCK=82000

DAEMON_OPTS="-a 192.168.12.246:80 

-T 127.0.0.1:3500 

-f /usr/local/varnish/etc/vcl.conf 

-u varnish -g varnish 

-w 2,51200,10 

-n /data/varnish/cache 

-s file, /data/varnish/cache/varnish_cache.data,4G"

这里需要说明的是,缓存文件“varnish_cache.data”在32位操作系统下,最大仅能支持2G,如果需要更大缓存文件则需要安装64为Linux操作系统。

接着需要修改的文件是/etc/init.d/varnish,找到如下行,改为相应的路径即可:

exec="/usr/local/varnish/sbin/varnishd"

prog="varnishd"

config="/etc/sysconfig/varnish"

lockfile="/var/lock/subsys/varnish"

其中,“exec”用于指定varnishd的路径,只需修改为varnish安装路径下对应的varnishd文件即可。“config”用于指定varnish守护进程配置文件路径。

两个文件修改完毕,就可以授权、运行/etc/init.d/varnish脚本了,执行如下:

[[email protected] ~]#chmod 755 /etc/init.d/varnish

[[email protected] ~]#/etc/init.d/varnish

Usage:/etc/init.d/varnish

{start|stop|status|restart|condrestart|try-restart|reload|force-reload}

从输出可知,此脚本功能强大,可以对varnish进行启动、关闭、查看状态、重启等操作。最后,启动varnish:

[[email protected] ~]# /etc/init.d/varnish  start

Starting varnish HTTP accelerator:                         [  OK  ]

三、管理varnish运行日志

varnish是通过内存共享的方式提供日志的,它提供了两种日志输出形式,分别是:

 通过自带的varnishlog指令可以获得varnish详细的系统运行日志。

例如:

[[email protected] ~]#/usr/local/varnish/bin/varnishlog -n /data/varnish/cache

0 CLI          - Rd ping

0 CLI          - Wr 200 PONG 1279032175 1.0

0 CLI          - Rd ping

0 CLI          - Wr 200 PONG 1279032178 1.0

通过自带的varnishncsa指令得到类似apache的combined输出格式的日志。

例如:

[[email protected] ~]#/usr/local/varnish/bin/varnishncsa  -n  /data/varnish/cache

也可以将日志输出到一个文件中,通过“-w”参数指定即可:

[[email protected] ~]#/usr/local/varnish/bin/varnishncsa -n /data/varnish/cache 

>-w /data/varnish/log/varnish.log

varnish两种日志输出形式中,第一种在大多数情况下并不是必须的,这里重点介绍下第二种日志输出形式的配置方式。

下面编写一个名为varnishncsa的shell脚本,并把此文件放到/etc/init.d目录下,varnishncsa脚本的完整内容如下所示:

#!/bin/sh

if [ "$1" = "start" ];then

/usr/local/varnish/bin/varnishncsa -n /data/varnish/cache  -f |/usr/sbin/rotatelogs /data/varnish/log/varnish.%Y.%m.%d.%H.log 3600 480 &

elif [ "$1" = "stop" ];then

killall varnishncsa

else

echo $0 "{start|stop}"

fi

在这个脚本中,通过管道方式把日志导入到“rotatelogs”中,而rotatelogs是一个文件分割工具,它可以通过指定时间或者大小等方式来分割日志文件,这样就避免了日志文件过大造成的性能问题。

其中,“3600”是一个小时,也就是每个小时生成一个日志文件,“480”是一个时区参数,中国是第八时区,相对于UTC相差480分钟,如果不设置480这个参数,将导致日志记录时间和服务器时间相差8小时。关于rotatelogs命令用法,这里不再详细讲述。

通过对varnish日志的监控,可以知道varnish的运行状态和情况。

接着,将此脚本进行授权:

[[email protected] ~]#chmod 755 /etc/init.d/varnishncsa

最后就可以通过如下方式,进行启动、关闭日志等操作了:

[[email protected] ~]#/etc/init.d/varnishncsa  {start|stop }

四、管理Varnish

1、查看varnish进程

通过上面章节的讲解,varnish已经可以启动起来了,执行如下命令可以查看varnish是否正常启动:

[[email protected] ~]# ps -ef|grep varnish

root     29615     1  0 00:20 pts/1    00:00:00 /usr/local/varnish/bin/varnishncsa -n /data/varnish/cache -f

root     29616     1  0 00:20 pts/1    00:00:00 /usr/sbin/rotatelogs /data/varnish/log/varnish.%Y.%m.%d.%H.log 3600 480

root     29646     1  0 00:21 ?        00:00:00 /usr/local/varnish/sbin/varnishd -P /var/run/varnish.pid -a 192.168.12.246:80 -T 127.0.0.1:3500 -f /usr/local/varnish/etc/vcl.conf -u varnish -g varnish -w 2,51200,10 -n /data/varnish/cache -s file,/data/varnish/cache/varnish_cache.data,4G

varnish  29647 29646  0 00:21 ?        00:00:00 /usr/local/varnish/sbin/varnishd -P /var/run/varnish.pid -a 192.168.12.246:80 -T 127.0.0.1:3500 -f /usr/local/varnish/etc/vcl.conf -u varnish -g varnish -w 2,51200,10 -n /data/varnish/cache -s file,/data/varnish/cache/varnish_cache.data,4G

从命令执行结果可知,PID为29615和29616的进程是varnish的日志输出进程,而PID为29646的进程是varnishd的主进程,并且派生出了一个PID为29647的子进程。这点跟apache类似。

如果varnish正常启动的话,80端口和3500端口应该处于监听状态,通过如下命令可以查看:

[[email protected] ~]# netstat -antl|grep 3500

tcp        0      0 127.0.0.1:3500              0.0.0.0:*                   LISTEN

[[email protected] ~]#netstat -antl|grep 80

tcp        0      0 192.168.12.246:80           0.0.0.0:*                   LISTEN

tcp        1     &nbs

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。