六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 571|回复: 0

安装配置Varnish3.0手记-it论坛-系统架构论坛-运维论坛

[复制链接]
 楼主| 发表于 2014-3-27 15:01:25 | 显示全部楼层 |阅读模式
安装配置Varnish3.0手记-it论坛-系统架构论坛-运维论坛


在内网的vps上安装Varnish的最新版3.0来用做测试
Varnish有centos下的rpm和ubuntu下的deb二进制包下载,但我选择的是源码编译安装
tar -xzvf varnish-3.0.0.tar.gz
yum install gcc
yum install gcc-c++ libstdc++-devel
yum install -y httpd-devel pcre perl pcre-devel zlib zlib-devel GeoIP GeoIP-devel

Varnish需要pcre支持,如果服务器没有安装pcre-devel会提示需要指定prce目录。
Centos服务器上使用yum install pcre-devel安装

解压varnish进行编译安装
cd varnish-3.0.0
./autogen.sh
./configure --prefix=/usr/local/varnish PKG_CONFIG_PATH=/usr/lib/pkgconfig
make
make install
安装成功后,安装的目录是
cd /usr/local/varnish/sbin
./varnishd -V
查看版本,看是否安装成功
测试varnish
先编辑配置文件
vi /usr/local/varnish/etc/varnish/default.vcl
  1. # This is a basic VCL configuration file for varnish.  See the vcl(7)
  2. # man page for details on VCL syntax and semantics.
  3. #
  4. # Default backend definition.  Set this to point to your content
  5. # server.
  6. #
  7. backend default {
  8.      .host = "192.168.88.156";
  9.      .port = "80";
  10.      ###下面三行为新加配
  11.      .connect_timeout = 1s;
  12.      .first_byte_timeout = 5s;
  13.      .between_bytes_timeout = 2s;
  14. }
  15. #
  16. # Below is a commented-out copy of the default VCL logic.  If you
  17. # redefine any of these subroutines, the built-in logic will be
  18. # appended to your code.
  19. sub vcl_recv {
  20.      if (req.restarts == 0) {
  21.         if (req.http.x-forwarded-for) {
  22.             set req.http.X-Forwarded-For =
  23.                 req.http.X-Forwarded-For + ", " + client.ip;
  24.         } else {
  25.             set req.http.X-Forwarded-For = client.ip;
  26.         }
  27.      }
  28.      if (req.request != "GET" &&
  29.        req.request != "HEAD" &&
  30.        req.request != "PUT" &&
  31.        req.request != "POST" &&
  32.        req.request != "TRACE" &&
  33.        req.request != "OPTIONS" &&
  34.        req.request != "DELETE") {
  35.          /* Non-RFC2616 or CONNECT which is weird. */
  36.          return (pipe);
  37.      }
  38.      if (req.request != "GET" && req.request != "HEAD") {
  39.          /* We only deal with GET and HEAD by default */
  40.          return (pass);
  41.      }
  42.      if (req.http.Authorization || req.http.Cookie) {
  43.          /* Not cacheable by default */
  44.          return (pass);
  45.      }
  46.      return (lookup);
  47. }
  48. #
  49. sub vcl_pipe {
  50. #     # Note that only the first request to the backend will have
  51. #     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
  52. #     # have it set for all requests, make sure to have:
  53. #     # set bereq.http.connection = "close";
  54. #     # here.  It is not set by default as it might break some broken web
  55. #     # applications, like IIS with NTLM authentication.
  56.      return (pipe);
  57. }
  58. #
  59. sub vcl_pass {
  60.      return (pass);
  61. }
  62. #
  63. sub vcl_hash {
  64.      hash_data(req.url);
  65.      if (req.http.host) {
  66.          hash_data(req.http.host);
  67.      } else {
  68.          hash_data(server.ip);
  69.      }
  70.      return (hash);
  71. }
  72. #
  73. sub vcl_hit {
  74.      return (deliver);
  75. }
  76. #
  77. sub vcl_miss {
  78.      return (fetch);
  79. }
  80. #
  81. sub vcl_fetch {
  82.      if (beresp.ttl <= 0s ||
  83.          beresp.http.Set-Cookie ||
  84.          beresp.http.Vary == "*") {
  85.                 /*
  86.                  * Mark as "Hit-For-Pass" for the next 2 minutes
  87.                  */
  88.                 set beresp.ttl = 120 s;
  89.                 return (hit_for_pass);
  90.      }
  91.      return (deliver);
  92. }
  93. #
  94. sub vcl_deliver {
  95.      return (deliver);
  96. }
  97. #
  98. # sub vcl_error {
  99. #     set obj.http.Content-Type = "text/html; charset=utf-8";
  100. #     set obj.http.Retry-After = "5";
  101. #     synthetic {"
  102. # <?xml version="1.0" encoding="utf-8"?>
  103. # <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  104. #  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  105. # <html>
  106. #   <head>
  107. #     <title>"} + obj.status + " " + obj.response + {"</title>
  108. #   </head>
  109. #   <body>
  110. #     <h1>Error "} + obj.status + " " + obj.response + {"</h1>
  111. #     <p>"} + obj.response + {"</p>
  112. #     <h3>Guru Meditation:</h3>
  113. #     <p>XID: "} + req.xid + {"</p>
  114. #     <hr>
  115. #     <p>Varnish cache server</p>
  116. #   </body>
  117. # </html>
  118. # "};
  119. #     return (deliver);
  120. # }
  121. #
  122. sub vcl_init {
  123.         return (ok);
  124. }
  125. #
  126. sub vcl_fini {
  127.         return (ok);
  128. }
复制代码
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,1024m -T 127.0.0.1:200 -a 0.0.0.0:80

现在可以打开varnish了
http://192.168.88.1/    (开启varnish的端口号)
戴图显示,nginx 为177上运行的web服务器,出现了varnish字样,表示缓存架设成功,已正常工作了。

启动varnishncsa用来将Varnish访问日志写入日志文件:
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish.log &


配置开机自动启动Varnish
vi /etc/rc.local

  在末尾增加以下内容:
引用
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &



 优化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


查看Varnish服务器连接数与命中率
/usr/local/varnish/bin/varnishstat

配置启动脚本:
mkdir /cache/varnish/V -p
vi /usr/local/varnish/sbin/start.sh

#!/bin/sh
#file:start.sh
date -u
/usr/local/varnish/sbin/varnishd -a 0.0.0.0:80  -s file,/cache/varnish/V,1024m  -f         /usr/local/varnish/etc/varnish/default.vcl  -p thread_pool_max=1500  -p thread_pools=5  -p listen_depth=512
chmod 777 start.sh
运行:
/usr/local/varnish/sbin/start.sh

安装配置Varnish3.0手记-it论坛-系统架构论坛-运维论坛
摘自:http://www.cnblogs.com/littlehb/archive/2012/02/11/2346319.html
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表