linux安装varnish3.0

post by rocdk890 / 2013-7-26 9:33 Friday linux技术
  最近看到squid已经支持多核了,再回过头来看看varnish,最新稳定版本还在3.0.4,当然这个是我们无法控制的,毕竟一个软件的更新是有开发的规律的,废话不多说了,让我们来看看怎么安装varnish吧.
  系统:centos 5.9
  需要的软件包:varnish-3.0.4.tar.gz

1.安装前准备
yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig 
yum -y install gcc* make

2.安装varnish
wget  http://repo.varnish-cache.org/source/varnish-3.0.4.tar.gz
tar zxf varnish-3.0.4.tar.gz && cd varnish-3.0.4
./configure --prefix=/usr/local/varnish
make && make install

3.varnish启动配置
groupadd varnish
useradd -g varnish -s /sbin/nologin varnish
cd varnish-3.0.4
cp redhat/varnish.sysconfig /etc/sysconfig/varnish
cp redhat/varnish.initrc /etc/init.d/varnish
cp redhat/varnish_reload_vcl /usr/local/varnish/bin/
vi /etc/profile
在最后加入:
export PATH=/usr/local/varnish/bin:$PATH
保存后,执行:
source /etc/profile

vi /etc/sysconfig/varnish
# Configuration file for varnish
#
# /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
# shell script fragment.
#

# Maximum number of open files (for ulimit -n)
NFILES=131072

# Locked shared memory (for ulimit -l)
# Default log size is 82MB + header
MEMLOCK=82000

# Maximum size of corefile (for ulimit -c). Default in Fedora is 0
# DAEMON_COREFILE_LIMIT="unlimited"

# Set this to 1 to make init script reload try to switch vcl without restart.
# To make this work, you need to set the following variables
# explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS,
# VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE, or in short,
# use Alternative 3, Advanced configuration, below
RELOAD_VCL=1

# This file contains 4 alternatives, please use only one.

## Alternative 1, Minimal configuration, no VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# content server on localhost:8080.  Use a fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -b localhost:8080 \
#             -u varnish -g varnish \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"


## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a
# fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -f /etc/varnish/default.vcl \
#             -u varnish -g varnish \
#             -S /etc/varnish/secret \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"


## Alternative 3, Advanced configuration
#
# See varnishd(1) for more information.
#
# # Main configuration file. You probably want to change it :)
#VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_VCL_CONF=/usr/local/varnish/etc/varnish/default.vcl
#
# # Default address and port to bind to
# # Blank address means all IPv4 and IPv6 interfaces, otherwise specify
# # a host name, an IPv4 dotted quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=
#VARNISH_LISTEN_PORT=6081
VARNISH_LISTEN_PORT=80
#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
#VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_SECRET_FILE=/usr/local/varnish/etc/varnish/secret
#
# # The minimum number of worker threads to start
#VARNISH_MIN_THREADS=1
VARNISH_MIN_THREADS=50
#
# # The Maximum number of worker threads to start
VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
VARNISH_STORAGE_SIZE=150M
#
# # Backend storage specification
#VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
VARNISH_TTL=120
#
# # DAEMON_OPTS is used by the init script.  If you add or remove options, make
# # sure you update this section, too.
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}"
#


## Alternative 4, Do It Yourself. See varnishd(1) for more information.
#
# DAEMON_OPTS=""

vi /etc/init.d/varnish
修改下面这三项:
pidfile=/var/run/varnish.pid
exec="/usr/local/varnish/sbin/varnishd"
reload_exec="/usr/local/varnish/bin/varnish_reload_vcl"

vi /usr/local/varnish/bin/varnish_reload_vcl
只修改下面这项,当然你也可以将/usr/local/varnish/bin添加到系统的PATH中,这样就不需要编辑varnish_reload_vcl

# Done parsing, set up command
VARNISHADM="/usr/local/varnish/bin/varnishadm $secret -T $VARNISH_ADMIN_LISTEN_ADDRESS:$VARNISH_ADMIN_LISTEN_PORT"

生成varnish管理秘钥:
/usr/bin/uuidgen > /usr/local/varnish/etc/varnish/secret
chmod 644 /usr/local/varnish/etc/varnish/secret

4.修改varnish配置文件
cp /usr/local/varnish/etc/varnish/default.vcl /usr/local/varnish/etc/varnish/default.vcl.old
vi /usr/local/varnish/etc/varnish/default.vcl
backend web01{  
 .host = "192.168.10.5";  
 .port = "80"; 
 .connect_timeout = 1s; 
 .first_byte_timeout = 5s;
 .between_bytes_timeout = 2s;  
 .probe = {
  .url = "/";
  .timeout = 0.3s;
  .window = 8;    
  .threshold = 3;  
  .initial = 3;  
 }  
}  
backend web02{  
 .host = "192.168.10.8";  
 .port = "80";  
 .connect_timeout = 1s;  
 .first_byte_timeout = 5s;  
 .between_bytes_timeout = 2s;  
 .probe = {  
  .url = "/";  
  .timeout = 0.3s;  
  .window = 8;  
  .threshold = 3;  
  .initial = 3;  
 }  
}  

director load round-robin {  
 {  
   .backend = web01;  
 }  
 {  
  .backend = web02;  
 }  
}  
 
acl purge{  
 "localhost";  
}  
 
 
sub vcl_recv{  
 if (req.request == "PURGE"){  
  if (!client.ip ~ purge){  
   error 405 "Not allowed.";  
  }  
  return (lookup);  
 }  
 if (req.http.host ~ "192.168.10.250"){  
  set req.backend = load;  
  }  
# if (req.restarts == 0){  
  if (req.http.x-forwarded-for) {  
   set req.http.X-Forwarded-For = 
   req.http.X-Forwarded-For + "," + client.ip;  
  }  
  else {  
   set req.http.X-Forwarded-For = client.ip;  
  }  
# }  
 if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") {  
  return (pipe);  
 }  
 elseif (req.request != "GET" && req.request != "HEAD") {  
  return (pass);  
 }  
 elseif  ((req.request=="GET" || req.request=="HEAD")&& req.url ~ "\.(png|gif|jpeg|swf|css|js|jpg)$"){  
  return (lookup);  
 }  
 else {  
  return (pipe);  
 }  
 if (req.http.Accept-Encoding){  
  if(req.url ~"\.(jpg|jpge|png|gif)$"){  
   #No point in compressing these  
   remove req.http.Accept-Encoding;  
  }  
  elseif (req.http.Accept-Encoding ~ "gzip"){  
   set req.http.Accept-Encoding = "gzip";  
  }  
  elseif (req.http.Accept-Encoding ~ "deflate"){  
   set req.http.Accept-Encoding = "deflate";  
  }  
  else{  
   #unknow algorithm  
   remove req.http.Accept-Encoding;  
  }  
 }  
}  
sub vcl_pipe {  
 return (pipe);  
}  
sub vcl_pass {  
 return (pass);  
}  
sub vcl_hash {  
 #set req.hash += req.url;  
 hash_data(req.url);
 if (req.http.host) {  
  #set req.hash += req.http.host;  
  hash_data(req.http.host);
 }  
 else {  
  #set req.hash += server.ip;
  hash_data(server.ip);  
 }  
 return (hash);  
}  
sub vcl_hit{  
# if (!obj.cacheable){  
#  return (pass);  
# }   
   if (req.request == "PURGE") {
       set obj.ttl = 0s;
       error 200 "Purged";
    }
 else {  
 return (deliver);  
 }  
}  
sub vcl_miss{  
 return (fetch);  
}  
sub vcl_fetch{  
# if (!beresp.cacheable) {  
#  return (pass);  
# }  
# elseif (beresp.http.Set-Cookie) {  
#  return (pass);  
# }  
# else {  
#  return (deliver);  
# }  
#}  
    if (beresp.ttl <= 0s ||
        beresp.http.Set-Cookie ||
        beresp.http.Vary == "*") {
                /*
                 * Mark as "Hit-For-Pass" for the next 2 minutes
                 */
                set beresp.ttl = 0 s;
                return (hit_for_pass);
    }
    if (beresp.http.Pragma ~"no-cache" ||
    beresp.http.Cache-Control ~"no-cache" ||
    beresp.http.Cache-Control ~"private") {
      return (deliver);
   }
    if (req.request == "GET"&&req.url ~ "(?i)\.(js|css|mp3|jpg|png|gif|swf|jpeg|ico)$") {
    set beresp.ttl = 30d;
  }
   if (req.request == "GET"&&req.url ~ "(?i)\.(html|htm)$") {
    set beresp.ttl = 1d;
  }
    return (deliver);
}
sub vcl_deliver {  
 return (deliver);  
}  
#sub vcl_deliver {
#     set resp.http.x-hits = obj.hits;
#     if (obj.hits > 0) {
#      set resp.http.X-Cache = "Hit test.com";
#   }else {
#       set resp.http.X-Cache = "Miss test.com";
#   }
#     set resp.http.Server = "BWM";
#     return (deliver);
# }
sub vcl_error {  
 set obj.http.Content-Type = "text/html; charset=utf-8";  
 synthetic {"  
<?xml version="1.0" encoding="utf-8" ?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html>  
 <head>  
  <title>"{ obj.status " " obj.response }"</title>  
 </head>  
 <body>  
  <h1>Error "{ obj.status "" obj.response }"</h1>  
  <p>"{ obj.response}"</p>  
  <h3>Guru Meditation:</h3>  
  <p>XID: "{req.xin}" </p>  
  <hr>  
  <p>Varnish cache server</p>  
 </body>  
</html>  
"};  
 return (deliver);  
 
}  
  
检查varnish配置文件是否有错误:
/etc/init.d/varnish configtest
或者
/usr/local/varnish/sbin/varnishd -C -f /usr/local/varnish/etc/varnish/fdfs.vcl

如果没有问题,就可以启动varnish
service varnish start

查看varnish状态
service varnish status
/etc/init.d/varnish status

动态加载varnish的VCL配置:
service varnish reload

5.配置varnish访问日志
cp redhat/varnishncsa.initrc /etc/init.d/varnishncsa
chmod +x /etc/init.d/varnishncsa
mkdir /var/log/varnish

vi /etc/init.d/varnishncsa
修改下面两项:
logfile="/var/log/varnish/varnishncsa.log"
exec="/usr/local/varnish/bin/varnishncsa"

启动varnishncsa:
service varnishncsa start

使用logrotate轮询日志文件(每天轮询,我这里设置为只保留了最近7天):
vi /etc/logrotate.d/varnish
/var/log/varnish/varnishncsa.log {
    missingok
    notifempty
    sharedscripts
    daily
    rotate 7
    dateext
    dateformat .%Y-%d-%m
    delaycompress
    postrotate
	    /bin/kill -HUP `cat /var/run/varnishncsa.pid 2>/dev/null` 2> /dev/null || true
    endscript
}

日志轮询debug测试:
logrotate -df /etc/logrotate.d/varnish

6.配置varnish开机自启动
chkconfig --add varnish
chkconfig varnish on
chkconfig --add varnishncsa
chkconfig varnishncsa on

7.varnish辅助命令
varnishtop 显示客户端最频繁请求的URL的持续更新列表:
varnishtop -i RxURL

显示最频繁命中后端的URL的持续更新列表:
varnishtop -b -i TxURL

varnishstat实时查看缓存统计信息(如:连接数和命中率):
varnishstat

仅查看特定项统计信息:
varnishstat -f client_conn,client_drop,client_req,cache_hit,cache_hitpass,cache_miss,backend_conn,backend_fail,backend_reuse,n_lru_nuked,n_wrk_lqueue,n_wrk_queued,n_wrk_drop

Debug缓存操作(ReqStart:后面为某客户端请求的IP):
varnishlog -c -m "ReqStart:192.168.1.120"

仅显示发送到后端的URLs(如:没命中缓存和内容还没被缓存):
varnishlog -O -i TxURL

将varnishlog写入到文件,分析访问特定页面的客户端请求:
varnishlog -w /var/log/varnish/varnishncsa.log -d
varnishlog -r /var/log/varnish/varnishncsa.log -c -m 'RxURL:^/test/c.php$'

查看varnishadm提供的命令:
varnishadm -T 127.0.0.1:6082 -S /usr/local/varnish/etc/varnish/secret help

查看后端服务器健康状况:
varnishadm debug.health
varnishadm backend.list
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-411.html

标签: 配置 安装 varnish linux varnish3.0

评论: