Varnish 缓存加速, Varnish 菜鸟看过来,Varnish实战

     Varnish可以有效降低web服务器的负载,提升访问速度按照HTTP协议的处理过程,web服务器接受请求并且返回处理结果,理想情况下服务器要在不做额外处理的情况下,立即返回结果,但实际情况并非如此。

当把Varnish部署上之后,web请求的处理过程会有一些变化。客户端的请求将首先被Varnish接受。Varnish将分析接收的请求,并将其转发到后端的web服务器上。后端的web服务器对请求进行常规的处理,并将依次将处理结果返回给Varnish

    但Varnish的功能并非仅限于此。Varnish的核心功能是能能将后端web服务器返回的结果缓存起来,如果发现后续有相同的请求,Varnish将不会将这个请求转发到web服务器,而是返回缓存中的结果。这将有效的降低web服务器的负载,提升响应速度,并且每秒可以响应更多的请求。Varnish速度很快的另一个主要原因是其缓存全部都是放在内存里的,这比放在磁盘上要快的多。诸如此类的优化措施使得Varnish的相应速度超乎想象。但考虑到实际的系统中内存一般是有限的,所以需要手工配置一下缓存的空间限额,同时避免缓存重复的内容。 

 

 

安装命令

 

sudo wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz

sudo tar -zxvf varnish-3.0.3.tar.gz 
cd varnish-3.0.3
sh autogen.sh 
sh configure
( make && make install )
make
make install
安装完毕。 修改配置信息。
cd etc/
vi default.vcl

启动命令. 

/usr/local/test/varnish-3.0.3/bin/varnishd/varnishd -f /usr/local/test/varnish-3.0.3/etc/default.vcl 

 

妈个鸡 竟然报错。

原来HTML 分开了行 他就不认识了。 语法跟JAVA拼接字符串一样。。

 

 -t 300 默认ttl时间存在5分钟(这个有点怪异,他是说从第一次来到他滚蛋总共有5分钟

/usr/local/test/varnish-3.0.3/bin/varnishd/varnishd -f /usr/local/test/varnish-3.0.3/etc/default.vcl -t 120s

 

 sub vcl_hit {

if(obj.hits>=100){ set obj.ttl=1h; }
# 哥 这么机制,当然再设置一次 说明这个货好牛,那我就多让他呆一会
if (obj.hits >= 30 ){ set obj.ttl=30m; } }
那么问题就来了,这里就变成了5分钟命中100次 才会改变他的缓存时间。 有点怪哦 = =!
这个需要好好考虑斟酌咯 根据业务改ttl时间

 

 

ps -ef | grep varnish
netstat -ntlp | grep varnish   查看 varnish 端口

 
 
curl -I http://127.0.0.1:80/pub/image/233/100115936744.png
 
x-cache  表示命中数
 

 
  1      从命令执行结果可知,PID为22378和22379的进程是varnish的日志输出进程,而PID为30685的进程是varnishd的主进程,并且派生出了一个PID为390的子进程。这点跟apache类似。
如果varnish正常启动的话,80端口和3500端口应该处于监听状态,通过如下命令可以查看:
netstat -antl|grep 80
netstat -antl|grep 3500
netstat -antl|grep 30001

        其中,80端口为varnish的代理端口,3500为varnish的管理端口。

  1. [root@varnish-server ~]# curl -I http://www.ixdba.net/a/mz/2010/0421/11.html  
  2. X-Cache: MISS from www.ixdba.net 

  #这里的"MISS"表示此次访问没有从缓存读取
再次打开这个页面,查看网页头信息:
 

  1. [root@varnish-server ~]# curl -I http://www.ixdba.net/a/mz/2010/0421/11.html  
  2. X-Cache: HIT from www.ixdba.net   

 #由“HIT”可知,第二次访问此页面时,是从缓存中读取内容了,也就是缓存命中。


   缓存命中率的高低直接说明了varnish的运行状态和效果,较高的缓存命中率说明了varnish运行状态良好,web服务器的性能也会提高很多,反之,过低的缓存命中率说明varnish的配置可能存在问题,那么就需要进行调整,因此,从整体上了解varnish的命中率和缓存状态,对于优化和调整varnish至关重要。
varnish提供了一个varnishstat命令,通过它可以帮我们获得很多重要的信息。
下面是一个varnish系统的缓存状态:

  1. [root@varnish-server ~]#/usr/local/varnish/bin/varnishstat  -n /data/varnish/cache  
  2. Hitrate ratio:       10      100      113  
  3. Hitrate avg:     0.9999   0.9964   0.9964  
  4.  
  5.         9990        68.92        49.70 Client connections accepted  
  6.       121820       953.84       606.07 Client requests received  
  7.       112801       919.88       561.20 Cache hits  
  8.           68         0.00         0.34 Cache misses  
  9.         2688        33.96        13.37 Backend conn. success  
  10.         6336         1.00        31.52 Backend conn. reuses  
  11.         2642        33.96        13.14 Backend conn. was closed  
  12.         8978        29.96        44.67 Backend conn. recycles  
  13.         6389         1.00        31.79 Fetch with Length  
  14.         2630        32.96        13.08 Fetch chunked  
  15.          444          .            .   N struct sess_mem  
  16.           23          .            .   N struct sess  
  17.           64          .            .   N struct object  
  18.           78          .            .   N struct objectcore  
  19.           78          .            .   N struct objecthead  
  20.          132          .            .   N struct smf  
  21.            2          .            .   N small free smf  
  22.            3          .            .   N large free smf  
  23.            6          .            .   N struct vbe_conn  
  24.           14          .            .   N worker threads  
  25.           68         1.00         0.34 N worker threads created  
  26.            0         0.00         0.00 N queued work requests  
  27.         1201        11.99         5.98 N overflowed work requests  
  28.            1          .            .   N backends  
  29.            4          .            .   N expired objects    
  30.         3701          .            .   N LRU moved objects  
  31.       118109       942.85       587.61 Objects sent with write  
  32.         9985        71.91        49.68 Total Sessions  
  33.       121820       953.84       606.07 Total Requests  

 

 

defaule 配置文件复制如下。。

 

  1. backend default {
  2. .host = "172.28.6.131";                    # 拦截服务器IP .port = "31001";                             # 端口号 .connect_timeout = 2s; .first_byte_timeout= 2s; }     sub vcl_recv { }
  3. sub vcl_deliver {
  4. set resp.http.x-cache=obj.hits; return (deliver); }
  5. # 显示x_cache 用。
  6. sub vcl_hit { if(obj.hits>=100){ set obj.ttl=1h; } } sub vcl_recv { if (req.request != "GET" && !req.url ~ ".(gif|jpg|swf|png|jpg)"){ return (pass); } }
 
 
default.vcl backend default { .host = "172.28.6.131"; .port = "31001"; .connect_timeout = 60s; .first_byte_timeout= 60s; # set obj.ttl = 300s; } sub vcl_recv { } sub vcl_deliver { set resp.http.x-cache=obj.hits; return (deliver); } sub vcl_hit { # -t 300 5 minute hit >= 10 30 minute; if(obj.hits>= 3 && obj.hits < 15){ set obj.ttl = 10m; } if (obj.hits >= 15 && obj.hits < 100 ){ set obj.ttl = 50m; } if (obj.hits > 100){ set obj.ttl = 3h; } } sub vcl_recv { if (req.request != "GET" && !req.url ~ ".(gif|jpg|swf|png|jpg)"){ return (pass); } }
 

 

 

 

 

传说中他是一个启动工具。 我没用过

wget -O varnishd https://raw.github.com/gist/3671408/3a51578bbd60a4cf8317bdc9508527b81eb23da5/varnishdcp varnishd /etc/init.d/varnishdchmod +x /etc/init.d/varnishd/etc/init.d/varnishd start

 

 

 

 

 

 

 

偷来的Varnish启动命令参数 varnishd指令
Varnish启动的命令是/usr/local/varnish/sbin/varnishd,此命令参数较多,用法比较复杂,在命令行执行“/usr/local/varnish/sbin/varnishd –h”即可得到varnishd的详细用法,表2-6列出了varnishd常用参数的使用方法和含义。
表1

  1. 命令参数 参数含义  
  2. -a address:port 表示varnish对httpd的监听地址及其端口  
  3. -b address:port 表示后端服务器地址及其端口  
  4. -d 表示使用debug调试模式  
  5. -f file 指定varnish服务器的配置文件  
  6. -p param=value 指定服务器参数,用来优化varnish性能  
  7. -P file Varnish进程PID文件存放路径  
  8. -n dir 指定varnish的工作目录  
  9. -s kind[,storageoptions] 指定varnish缓存内容的存放方式,常用的方式有:“-s file,<dir_or_file>,<size>”。  
  10. 其中“<dir_or_file>”指定缓存文件的存放路径,“<size>”指定缓存文件的大小  
  11. -t 指定缺省的TTL值  
  12. -T address:port 设定varnish的telnet管理地址及其端口  
  13. -w int[,int[,int]] 设定varnish的工作线程数,常用的方式有:  
  14. -w min,max  
  15. -w min,max,timeout  
  16. 例如:-w5,51200,30,这里需要说明下,在varnish2.0版本以后,最小启动的线程数不能设定过大,设置过大,会导致varnish运行异常缓慢。
    -V 显示varnish版本号和版权信息 

 

 

 

 

 

 

 

 

 

 

        God has given me a gift. Only one. I am the most complete fighter in the world. My whole life, I have trained. I must prove I am worthy of someting.                                                             rocky_24

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 





God has given me a gift. Only one. I am the most complete fighter in the world. My whole life, I have trained. I must prove I am worthy of someting. rocky_24
原文地址:https://www.cnblogs.com/rocky24/p/a07ef67ef093c646a563c25b92b80e14.html