CentOS 5.8 下如何安装配置 Varnish-2.1.5 ?

原创
运维 系统运维
Varnish是一款强大的反向代理加速软件,其工作原理、具体流程及VCL语法网上资料多,本文不再赘述。主要讲述了在CentOS5.8 下,对varnish-2.1.5的安装配置方法。

【51CTO原创稿件】Varnish是一款强大的反向代理加速软件,关于其工作原理可以参考下图,其具体流程及VCL语法我这里就不做说明,网上资料多,大家还可以对照参考其官方网站和《Varnish中文权威指南》:

 

一、安装CentOS5.8系统环境下的依耐关系

  1. yum install gcc gcc-c++ 
  2. yum install automake autoconflibtool ncurses-devel libxslt groff pcre-devel pkgconfig libtool -y 

二、下载varnish-2.1.5源码包,并进行编译安装。

  1. cd /usr/local/src 
  2. wget  http://repo.varnish-cache.org/source/varnish-2.1.5.tar.gz 
  3. tar zxvf varnish-2.1.5.tar.gz 
  4. cd varnish-2.1.5. 
  5. ./autogen.sh 

#autogen.sh命令是用来检查软件的依耐关系是否满足,如果报错的话, 则应该如下正常所示:

  1. + aclocal 
  2. + libtoolize --copy --force 
  3. + autoheader 
  4. + automake --add-missing --copy --foreign 
  5. + autoconf 

继续编译安装:

  1. ./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings 
  2. make && make install && cd ../ 

三、创建varnish用户和组,以及varnish缓存文件和日志存放目录:

  1. /usr/sbin/groupadd varnish 
  2. /usr/sbin/useradd -s /sbin/nologin  -g varnish varnish 
  3. mkdir -p /data/varnish/{cache,log} 
  4. chown  -R varnish:varnish /data/varnish/{cache,log} 

四、我的测试环境是两台Web机器,IP为192.168.1.103(域名为http://www.yuhongchun027.net)的varnish机器对后端IP为192.168.1.104和192.168.1.105的机器进行反向代理加速,其配置文件/usr/local/varnish/etc/varnish/better.vcl如下所示:

  1. backend rserver1 
  2. .host ="192.168.1.104"
  3. .port = "80"
  4. .probe = { 
  5. .timeout = 5s;          #等待多长时间超时 
  6. .interval = 2s;          #检查时间间隔 
  7. .window = 10;         #varnish将维持10个sliding windows的结果 
  8. .threshold = 8;         #如果是8次.windows检查是成功的,就宣告后端的Web机器是健康的 
  9. backend rserver2 
  10. .host ="192.168.1.105"
  11. .port = "80"
  12. .probe = { 
  13. .timeout = 5s
  14. .interval = 2s
  15. .window = 10
  16. .threshold = 8

#指定一个名为realserver组,使用random机制,权重越大,分配的访问越多,可根据服务器性能来设定;而round-robin(轮询)机制是不能指定weight的

  1. director realserver random { 
  2. .backend = rserver1
  3. .weight = 5
  4. .backend = rserver2
  5. .weight = 6

#定义能清理缓存的机器,这里只允许本机能用purge的方式清理

  1. acl purge { 
  2. "localhost"; 
  3. "127.0.0.1"; 
  4. sub vcl_recv 
  5. if (req.http.host ~"^(.*).yuhongchun027.net") 
  6. set req.backend =realserver
  7. else 
  8. error 200 "Nocahce for this domain"; 
  9. if (req.request =="PURGE") 
  10. if (!client.ip ~purge) 
  11. error 405"Not allowed."; 
  12. else 
  13. return (pipe); 

#获取客户端真实IP地址

  1. if(req.http.x-forwarded-for) 
  2. set reqreq.http.X-Forwarded-For = 
  3. req.http.X-Forwarded-For "," client.ip; 
  4. else 
  5. set req.http.X-Forwarded-For =client.ip; 

#对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;

  1. if (req.request !="GET" && req.request != "HEAD") 
  2. return (pipe); 
  3. if (req.http.Expect) 
  4. return (pipe); 
  5. if (req.http.Authenticate|| req.http.Cookie) 
  6. return (pass); 
  7. if (req.http.Cache-Control~ "no-cache") 
  8. return (pass); 

#对JSP或者PHP文件不缓存

  1. if(req.url ~"\.jsp" || req.url ~ "\.php" ) 
  2. return (pass); 
  3. else 
  4. return (lookup); 
  5. }sub vcl_pipe 
  6. return (pipe); 
  7. }sub vcl_pass 
  8. return (pass); 
  9. }sub vcl_hash 
  10. set req.hash += req.url; 
  11. if (req.http.host) 
  12. set req.hash +=req.http.host; 
  13. else 
  14. set req.hash +=server.ip; 
  15. return (hash); 
  16. }sub vcl_hit 
  17. if (req.request =="PURGE") 
  18. set obj.ttl = 0s
  19. error 200"Purged."; 
  20. if (!obj.cacheable) 
  21. return (pass); 
  22. return (deliver); 
  23. }sub vcl_miss 
  24. if (req.request =="PURGE") 
  25. error 404 "Not incache."; 
  26. if (req.http.user-agent ~"spider") 
  27. error 503 "Notpresently in cache"; 
  28. return (fetch); 
  29. sub vcl_fetch 
  30. if (req.request =="GET" && req.url ~ "\.(txt|js)$") 
  31. set beresp.ttl = 3600s
  32. else 
  33. set beresp.ttl = 30d
  34. if (!beresp.cacheable) 
  35. return (pass); 
  36. if (beresp.http.Set-Cookie) 
  37. return (pass); 
  38. return (deliver); 
  39. sub vcl_deliver { 
  40. if (obj.hits > 0) { 
  41. set resp.http.X-Cache"HIT  FROM www.yuhongchun027.net"
  42. } else { 
  43. set resp.http.X-Cache"MISS FROM www.yuhongchun027.net"
  44. return (deliver); 

五、启动varnish的命令很长,如下所示:

 /usr/local/varnish/sbin/varnishd -n /data/varnish/cache -f /usr/local/varnish/etc/varnish/better.vcl -a 0.0.0.0:80 -s file,/data/varnish/varnish_cache.data,8G  -p user=varnish -p group=varnish -p default_ttl=14400 -p thread_pool_max=8000 -p send_timeout=20 -w 5,51200,30 -T 127.0.0.1:3500  -p /usr/local/varnish/var/varnish.pid

验证其是否生效可以用curl -I命令,如下所示:

  1. [root@localhost cache]# curl -I http://www.yuhongchun027.net/ 
  2. HTTP/1.1 200 OK 
  3. Server: Apache/2.2.3 (CentOS) 
  4. Last-Modified: Wed, 28 Aug 2013 16:27:33 GMT 
  5. ETag: "10d242-e-776b6740" 
  6. Content-Type: text/html; charset=UTF-8 
  7. Content-Length: 14 
  8. Date: Wed, 21 Aug 2013 17:47:48 GMT 
  9. X-Varnish: 1584727079 1584726982 
  10. Age: 10101 
  11. Via: 1.1 varnish 
  12. Connection: keep-alive 
  13. X-Cache: HIT  FROM www.yuhongchun027.net 

六、如果vcl配置文件发生改动,想要不重启而直接reload,可以用如下操作,可以在本机上进行telnet操作,连接3500管理端口:

  1. telnet 127.0.0.1 3500 
  2. vcl.load newconfig /usr/local/varnish/etc/varnish/better.vcl 
  3. 200 13 
  4. VCL compiled. 
  5. vcl.use newconfig 
  6. 200 0 

如果显示有200字样,则表示已经正常reload了,newconfig这个名字是自己定义的,熟悉varnish操作的朋友应该也清楚,通过telnet连接本机还可以进行清理缓存。

七、用varnishadm命令来清理缓存,例子如下所示:

清除所有缓存:

  1. /usr/local/varnish/bin/varnishadm -T 192.168.1.103:3500 url.purge *$ 

清除image目录下所有缓存:

  1. /usr/local/varnish/bin/varnishadm -T 192.168.1.103:3500 url.purge /image/ 

查看最近清除的详细url列表,可执行如下命令:

  1. /usr/local/varnish/bin/varnishadm -T 192.168.1.103:3500 purge.list 

另外,缓存命中率的高低直接说明了varnish的运行状态和效果,如果缓存率命中率过低,我们应该对varnish配置进行检查调整来进行提高,查看其命中率命令如下所示:

  1. /usr/local/varnish/bin/varnishstat -n /data/varnish/cache 

八、内核优化如下所示:

编辑/etc/sysctl.conf,添加如下选项:

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

net.ipv4.tcp_max_syn_backlog = 65536

net.core.netdev_max_backlog =  32768

net.core.somaxconn = 32768

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 2

net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_max_orphans = 3276800

执行如下命令,让改动配置立即生效:

  1. /sbin/sysctl -p 

注意:老生常谈的ulimit的问题,这个话题说得太多了,这里实在不想再提了,记得将

  1. ulimit -SHn 65535 

放在/etc/rc.local里即可,记得在启动varnish之前将此命令手动执行一遍,另外,在工作中发现,CentOS6.x x86_64下更改ulimit跟CentOS5.x x86_64略有不同,这点也请大家注意。

以上即为varnish-2.1.5在CentOS5.8下的安装配置过程,记录下作为工作笔记,年纪大了,起个备忘作用而矣。

 

个人博客:http://andrewyu.blog.51cto.com

微博地址:http://weibo.com/yuhongchun027

【声明】本文作者:余洪春(抚琴煮酒),英文名Andrew.Yu。在51CTO系统频道首发,转载请注明作者和出处。

 

责任编辑:黄丹 来源: 51CTO.com
相关推荐

2012-10-11 14:59:31

CentosFastDFS

2011-04-01 12:22:35

2011-02-23 09:55:33

Centos安装vsftpd

2011-03-31 09:02:22

Windows安装MRTG

2011-04-01 14:09:44

CentOS 5.5zabbix

2011-03-02 09:47:51

PureftpCentos

2011-03-02 10:09:53

CentosPureftp

2012-04-02 15:29:00

httpvarnish

2010-05-21 15:12:58

Centos下SVN的

2012-03-18 22:33:15

centosxen

2011-03-31 10:31:18

Ubuntu安装MRTG

2011-03-11 12:57:30

CentosLAMP

2010-01-13 11:17:50

2010-03-29 14:48:58

CentOS系统

2010-01-14 13:47:30

CentOS extm

2010-01-13 15:50:23

CentOS安装

2012-08-17 09:48:55

MongoDB

2011-09-02 14:07:59

2021-01-12 09:55:29

LinuxCntlm代理

2020-09-25 17:10:13

LinuxMySQL 8.0
点赞
收藏

51CTO技术栈公众号