Varnish构建Cache代理服务器笔记
Varnish是一个非常不错的HTTP accelerator,具体我也不做多方面介绍了大家可以亲自尝试一下,just do it!"停止一切空谈!"。
以下是我配置服务的过程贴出来和朋友们一起分享。在调试过程中感谢手机之家张建(ajian)[手之家目前利用他来代替Squid作为图片缓存服务器性能表现非常不错!~]帮忙调试排除问题!同时欢迎朋友们与我分享你的心得!
一.Varnish安装
wget http://downloads.sourceforge.net/varnish/varnish-2.0.2.tar.gz?modtime=1226669272&big_mirror=0
mkdir /data/cache
chown www.www /data/cache/ -R
chmod a+w /data/cache/ -R
./configure --prefix=/usr/local/varnish && make && make install
二.配置vcl.conf
#vi /usr/local/varnish/etc/varnish/vcl.conf #贴出我的多域名虚拟主机配置,不做细节介绍.
#Cache for linuxtone sites
#backend vhost
backend img {
.host = "219.235.244.11";
.port = "8080";
}
backend www {
.host = "211.101.76.140";
.port = "80";
}
backend netseek {
.host = "211.101.76.141";
.port = "80";
}
#acl
acl purge {
"localhost";
"127.0.0.1";
"192.168.169.0"/24;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
lookup;
}
if (req.http.host ~ "^imgsns.linuxtone.org") {
set req.backend = img;}
elseif (req.http.host ~ "^(www)|(bbs)|(doc).linuxtone.org") {
set req.backend = www;}
elseif (req.http.host ~ ".netseek.com") {
set req.backend = netseek;}
else {
error 404 "the server is wrong!";
}
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
elseif (req.url ~ "\.(php|cgi)($|\?)")
{
pass;
}
lookup;
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
}
sub vcl_fetch {
if (req.request == "GET" && req.url ~ "\.(txt|js|css)$") {
set obj.ttl = 10s;
}
else {
set obj.ttl = 1d;
}
}
三.Varnish启动脚本制作
1.启动脚本:
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/vcl.conf -a 0.0.0.0:80 \
-n /var/vcache \
-g www -u www \
-T 127.0.0.1:6082 \
-p thread_pool_max=2048 \
-p thread_pools=4 \
-p client_http11=on \
-s file,/data/cache/varnish_cache.data,1024M
2.制作成系统服务启动脚本
详细参见:
注:varnish启动脚本
1.在安装源文件目录下
cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
chmod 755 /etc/init.d/varnish
chkconfig --add varnish
chkconfig --level 345 varnish on
#vi /etc/init.d/varnish
#! /bin/sh
#
# varnish Control the varnish HTTP accelerator
#
# chkconfig: - 90 10
# description: Varnish is a high-perfomance HTTP accelerator
# processname: varnishd
# config: /etc/sysconfig/varnish
# pidfile: /var/run/varnish/varnishd.pid
### BEGIN INIT INFO
# Provides: varnish
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog
# Short-Description: start and stop varnishd
# Description: Varnish is a high-perfomance HTTP accelerator
### END INIT INFO
# Source function library.
. /etc/init.d/functions
retval=0
pidfile=/var/run/varnish.pid
exec="/usr/local/varnish/sbin/varnishd" #需要修改成你安装的路径
prog="varnishd"
config="/etc/sysconfig/varnish"
lockfile="/var/lock/subsys/varnish"
# Include varnish defaults
[ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish
start() {
if [ ! -x $exec ]
then
echo $exec not found
exit 5
fi
if [ ! -f $config ]
then
echo $config not found
exit 6
fi
echo -n "Starting varnish HTTP accelerator: "
# Open files (usually 1024, which is way too small for varnish)
ulimit -n ${NFILES:-131072}
# Varnish wants to lock shared memory log in memory.
ulimit -l ${MEMLOCK:-82000}
#ulimit
ulimit -SHn 51200
# $DAEMON_OPTS is set in /etc/sysconfig/varnish. At least, one
# has to set up a backend, or /tmp will be used, which is a bad idea.
if [ "$DAEMON_OPTS" = "" ]; then
echo "\$DAEMON_OPTS empty."
echo -n "Please put configuration options in $config"
return 6
else
# Varnish always gives output on STDOUT
daemon --pidfile $pidfile $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
retval=$?
if [ $retval -eq 0 ]
then
touch $lockfile
echo_success
echo
else
echo_failure
fi
return $retval
fi
}
stop() {
echo -n "Stopping varnish HTTP accelerator: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
reload() {
restart
}
force_reload() {
restart
}
rh_status() {
status -p $pidfile $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# See how we were called.
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
#vi /etc/sysconfig/varnish
NFILES=131072
MEMLOCK=82000
DAEMON_OPTS="-a 0.0.0.0:80 \
-T 127.0.0.1:6082 \
-f /usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/vcl.conf \
-u www -g www \
-p thread_pool_max=2048 \
-p thread_pools=4 \
-p client_http11=on \
-s file,/data/cache/varnish_cache.data,1G"
# /etc/init.d/varnish restart
Stopping varnish HTTP accelerator: [ OK ]
Starting varnish HTTP accelerator: [ OK ]
四.Varnish管理
1.通过端口进行管理
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:6082 help
2.查看varnish缓存状态
/usr/local/varnish/bin/varnishstat
3.通过管理端口清除cache
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:6082 purge.url <regexp>
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:6082 purge.list #列出刚才清除的具体URL列表.
4.日志记录
/usr/local/varnish/bin/varnishncsa -a -w /usr/local/varnish/logs/varnish.log &
varnish日志的rotate
touch /etc/logrotate.d/varnish
vi /etc/logrotate.d/varnish
/usr/local/varnish/logs/varnish.log {
daily
rotate 60
copytruncate
notifempty
missingok
prerotate
killall varnishncsa
endscript
postrotate
/usr/local/varnish/bin/varnishncsa -a -w /usr/local/varnish/logs/varnish.log &
endscript
}
五.Varnish及其相关参考.
1.Varnish 基本语法:
2.Varnish 官方资源:
- 最新评论