- 论坛徽章:
- 2
|
操作步骤
cd
useradd -d /dev/null -s /sbin/nologin
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
逐步说明
增加一个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内容
[color="#0000ff"]backend www1 {
set backend.host = "后端真实的web IP1";
set backend.port = "http";
}
[color="#0000ff"]backend www2 {
set backend.host = "后端真实的web IP2";
set backend.port = "http";
}
[color="#0000ff"]backend www3 {
set backend.host = "后端真实的web IP2";
set backend.port = "8080";
}
#可以做多个类似的后端
[color="#0000ff"]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";
}
}
[color="#0000ff"] 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;
}
[color="#0000ff"]sub vcl_pipe {
pipe;
}
[color="#0000ff"]sub vcl_pass {
pass;
}
[color="#0000ff"]sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.host;
#这里看情况,影响缓存命中率
#set req.hash += req.http.cookie;
hash;
}
[color="#0000ff"]sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
if (!obj.cacheable) {
pass;
}
deliver;
}
[color="#0000ff"]sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
fetch;
}
[color="#0000ff"]sub vcl_fetch {
if (!obj.valid) {
error;
}
if (!obj.cacheable) {
pass;
}
[color="#0000ff"] 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;
}
[color="#0000ff"]sub vcl_deliver {
deliver;
}
[color="#0000ff"]sub vcl_timeout {
discard;
}
[color="#0000ff"]sub vcl_discard {
discard;
}
启动
脚本
/usr/local/varnish/startv内容
[color="#0000ff"]#侦听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内容
[color="#0000ff"]killall varnishd
killall varnishncsa
重启脚本(重新读取配置)
[color="#0000ff"]/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内容
[color="#0000ff"]/usr/local/varnish/logs/varnish.log {
daily
rotate 60
copytruncate
notifempty
missingok
[color="#0000ff"] prerotate
killall varnishncsa
endscript
[color="#0000ff"] postrotate
/usr/local/varnish/bin/varnishncsa -a -w /usr/local/varnish/logs/varnish.log &
endscript
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4206/showart_505522.html |
|