varnish cache 反向代理服务器和http加速器的安装和配置_湖南博益科技有限公司 
立即收藏 中文 英文

联系方式

  • 网址:www.it09.com
  • 电话:15073922433
  • 地址:长沙市国储城市天地1502
  • Email:www_it09_com@126.com
  • QQ:
  • 点击这里给我发消息
  • 点击这里给我发消息
  • 当前位置:>湖南博益科技有限公司 > varnish cache 反向代理服务器和http加速器的安装和配置

    varnish cache 反向代理服务器和http加速器的安装和配置

    点击次数:99更新时间:2011-05-04 19:26:28【打印】【关闭】

       

      一,什么是varnish

      Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。
      Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算 机系统的内存除了主存外,还包括了CPU内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此Squid Cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构。
      Varnish的理念是好的,但是Varnish还没有稳定到可以完全替代Squid的程度,现在就抛弃Squid选择Varnish是不明智的。

       

      二,安装varnish

      http://sourceforge.net/projects/varnish/files/
      tar zxvf varnish-2.1.tar.gz
      cd varnish-2.1
      ./configure –prefix=/usr/local/varnish
      make && make install

      三,varnish的配置文件

      vi /usr/local/varnish/vcl.conf

      输入以下内容

      1. backend testserver {  
      2.        set backend.host = "127.0.0.1";  
      3.        set backend.port = "10000";  
      4. }  
      5.   
      6. backend imageserver {  
      7.        set backend.host = "127.0.0.1";  
      8.        set backend.port = "10001";  
      9. }  
      10.   
      11. acl purge {  
      12.     "localhost";  
      13.        "127.0.0.1";  
      14.        "192.168.1.0"/24;  
      15. }  
      16.   
      17. sub vcl_recv {  
      18.        if (req.request == "PURGE") {  
      19.             if (!client.ip ~ purge) {  
      20.                    error 405 "Not allowed.";  
      21.                      }  
      22.                lookup;  
      23.              }  
      24.   
      25.        if (req.http.host ~ "^localhost") {  
      26.                set req.backend = testserver;  
      27.   
      28.        } elseif (req.http.host ~ "^127.0.0.1") {  
      29.                set req.backend = imageserver;  
      30.   
      31.        } else{  
      32.                error 404 "test Cache Server";  
      33.              }  
      34.   
      35.            if (req.request != "GET" && req.request != "HEAD") {  
      36.                            pipe;  
      37.                    }  
      38.             if (req.http.Expect) {  
      39.                 pipe;  
      40.             }  
      41.             if (req.http.Authenticate || req.http.Cookie || req.url ~ "\.(php|cgi)($|\?)") {  
      42.                 pass;  
      43.             }  
      44.                 if (req.request == "GET" && req.url ~ "\.(txt|js|jpg|gif|png|bmp|css)$") {  
      45.                         lookup;  
      46.                 }  
      47.         lookup;  
      48. }  
      49.   
      50. sub vcl_hit {  
      51.        if (req.request == "PURGE") {  
      52.                set obj.ttl = 0s;  
      53.                error 200 "Purged.";  
      54.        }  
      55. }  
      56.   
      57. sub vcl_miss {  
      58.        if (req.request == "PURGE") {  
      59.                error 404 "Not in cache.";  
      60.        }  
      61. }  
      62.   
      63. sub vcl_fetch {  
      64.        if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private")  
      65.        {  
      66.               pass;  
      67.        }  
      68.        if (req.request == "GET" && req.url ~ "\.(txt|js|jpg|gif|png|bmp|css)$") {  
      69.                set obj.ttl = 3600s;  
      70.        }  
      71.        else {  
      72.                set obj.ttl = 30d;  
      73.        }  
      74. }  
      75.   
      76. sub vcl_hash  
      77. {  
      78.        set req.hash += req.url;  
      79.        if (req.http.host)  
      80.        {  
      81.        set req.hash += req.http.host;  
      82.        }else{  
      83.      set req.hash += server.ip;  
      84.        }  
      85.        hash;  
      86. }  
      87.   
      88. sub vcl_pipe {  
      89.     pipe;  
      90. }   
      91.   
      92. sub vcl_pass {  
      93.     pass;  
      94. }  

      简单说明一下:

      1,设置反向请求的IP,127.0.0.1:10000和127.0.0.1:10001

      2,允许缓存管理,localhost,127.0.0.1,192.168.0.*

      3,varnish对匹配localhost请求跳到testserver,对匹配127.0.0.1的请求跳 到imageserver,如果都没有,显示test Cache Server

      4,Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。放过php,cgi不对其进行缓存,只缓存,静态文件

      5,对以.txt.js.jpg.gif.png.bmp.css结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。

      四,优化Linux内核参数

      vi /etc/sysctl.conf

      加入

      net.ipv4.tcp_fin_timeout = 30
      net.ipv4.tcp_keepalive_time = 300
      net.ipv4.tcp_syncookies = 1
      net.ipv4.tcp_tw_reuse = 1
      net.ipv4.tcp_tw_recycle = 1
      net.ipv4.ip_local_port_range = 5000    65000

      ldconfig进行重新加载

      五,启动varnish和varnishncsa,并查看

      ulimit -SHn 51200
      /usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a :8080 -s file,/var/vcache/varnish_cache.data,1G -g users -u zhangy -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
      /usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/vlogs/varnish.log &

      解释一下8080是varnish监听的端口,例如127.0.0.1:8080会反向请求127.0.0.1:10001图片服务器,127.0.0.1:3500这个是管理缓存的ip

      [zhangy@BlackGhost ~]$ ps -e|grep varnish
      3970 ?        00:00:00 varnishd
      3971 ?        00:00:00 varnishd
      3977 pts/0    00:00:00 varnishncsa

      六、通过Varnish管理端口,使用正则表达式批量清除缓存:

      1,例:清除类似http://127.0.0.1:8080/00/01的URL地址:

      [root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /00/

      2,例:清除类似http://127.0.0.1:8080/00/0144的URL地址:

      [root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge \d*$

      3,例:清除类似http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif的url地址

      [root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge gif$

      4,清除所有

      [root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$

      5,怎么测试你知道有没有清除缓存

      在地址栏输入http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif
      然后涮新几次你发现以下内容

      清缓存前

      清缓存前

      说明:请求了8次,后台请求1次,缓存7次

      清除缓存
      [root@BlackGhost varnish]# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge gif$

      然后在请求http://127.0.0.1:8080/00/01/RwGowEtWvcQAAAAAAAAWHH0Rklg81.gif

      你会发现以下内容

      缓存清后

      缓存清后

      说明:请求数变了,缓存数没变,后台请求数变了,说明刚才请缓存是有效果的。

      七,进行缓存测试,以及部分参数说明

      varnish

      varnish

      上图中199719表示用户请求数;

      上图中199704表示用户请求收到数

      上图中199688表示这个缓存命中次数,就是缓了多少次

      上图中第一个2表示没有缓存数,第一次访问都不会被缓存的,memcache也是

      上图中第二个2表示后台处理数,没缓存当然要交到后台处理

    相关导航

  • 2011最给力的115条让人喷饭句子
  • php通过正则获取网站图片
  • 
    湖南博益科技有限公司 版权所有,侵权必究

    powerd by 博益科技©版权归胡芳强个人所有©

    该程式共运行了0秒263毫秒508微秒。