使用Varnish反向代理缓存的安装配置
增加一个varnish帐户
useradd -d /dev/null -s /sbin/nologin varnish
下载varnish
wget http://nchc.dl.sourceforge.net/sourceforge/varnish/varnish-1.1.1.tar.gz
解压
tar zxvf varnish-1.1.1.tar.gz
cd varnish-1.1.1
配置
./configure –prefix=/usr/local/varnish
编译
make
安装
make install
设置相关目录
cd /usr/local/varnish
mkdir var
mkdir logs
mkdir etc
mkdir cache
配置文件
touch /usr/local/varnish/etc/cache.vcl
touch /usr/local/varnish/stopv
touch /usr/local/varnish/startv
touch /usr/local/varnish/resetv
chmod +x /usr/local/varnish/stopv
chmod +x /usr/local/varnish/startv
chmod +x /usr/local/varnish/resetv
修改权限
cd ..
chown -R varnish varnish
chgrp -R varnish varnish
cache.vcl内容
backend www1 {
set backend.host = “后端真实的web IP1″;
set backend.port = “http”;
}
backend www2 {
set backend.host = “后端真实的web IP2″;
set backend.port = “http”;
}
backend www3 {
set backend.host = “后端真实的web IP2″;
set backend.port = “8080″;
}
#可以做多个类似的后端
acl purge {
“localhost”;
“后端真实的web IP1″;
“后端真实的web IP2″;
}
sub vcl_recv {
if (req.http.host ~ “域名1″) {
set req.backend = www1;
}
else
{
if(req.http.host ~ “域名2″){
set req.backend = www2;
}
else{
error 200 “No cahce for this domain”;
}
}
if (req.request == “PURGE”) {
if (!client.ip ~ purge) {
error 405 “Not allowed.”;
}
else{
lookup;
}
}
if (req.request != “GET” && req.request != “HEAD”) {
pipe;
}
if (req.http.Expect) {
pipe;
}
if (req.http.Authenticate ){
pass;
}
if (req.http.Cache-Control ~ “no-cache”) {
pass;
}
#对ASP或者PHP文件不缓存
if(req.url ~ “\.asp” || req.url ~ “\.php” ){
pass;
}
lookup;
}
sub vcl_pipe {
pipe;
}
sub vcl_pass {
pass;
}
sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.host;
#这里看情况,影响缓存命中率
#set req.hash += req.http.cookie;
hash;
}
sub vcl_hit {
if (req.request == “PURGE”) {
set obj.ttl = 0s;
error 200 “Purged.”;
}
if (!obj.cacheable) {
pass;
}
deliver;
}
sub vcl_miss {
if (req.request == “PURGE”) {
error 404 “Not in cache.”;
}
fetch;
}
sub vcl_fetch {
if (!obj.valid) {
error;
}
if (!obj.cacheable) {
pass;
}
if (obj.http.Pragma ~ “no-cache” || obj.http.Cache-Control ~ “no-cache” || obj.http.Cache-Control ~ “private”) {
pass;
}
if (obj.ttl < 180s) {
set obj.ttl = 180s;
}
insert;
}
sub vcl_deliver {
deliver;
}
sub vcl_timeout {
discard;
}
sub vcl_discard {
discard;
}
?
启动脚本
/usr/local/varnish/startv内容
#侦听80端口
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/cache.vcl \
-a 0.0.0.0:80 \
-s file,/usr/local/varnish/cache,1024M \
-p user=varnish -p group=varnish \
-p default_ttl=14400 -p thread_pool_max=8000 -p send_timeout=20 \
-p backend_http11=on -p client_http11=on \
-p srcaddr_ttl=720 \
-w 4000,12000,10 -T 127.0.0.1:808 \
-P /usr/local/varnish/var/varnish.pid
#日志记录
/usr/local/varnish/bin/varnishncsa -a -w /usr/local/varnish/logs/varnish.log &
停止脚本
/usr/local/varnish/stopvn内容
killall varnishd
killall varnishncsa
重启脚本(重新读取配置)
/usr/local/varnish/resetv内容
kill -HUP `cat/usr/local/varnish/var/varnish.pid`
运行/usr/local/varnish/startv启动缓存
日志记录在/usr/local/varnish/logs/varnish.log
启动后可以通过在本机telnet 127.0.0.1 808连接varnish的管理端口,查看运行状态等
补充一下:
varnish日志的rotate
touch /etc/logrotate.d/varnish
/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
}


评论
还没有评论。
发表评论