Varnish配置筆記記錄

來源:互聯網
上載者:User

   Varnish是一個開源的反向 Proxy軟體和HTTP加速器,與傳統的Squid相比,Varnish具有效能更高、速度更快、管理更方便等諸多優點,很多大型的運營網站都開始嘗試用Varnish來替換Squid,這些都促使Varnish迅速發展起來。

  1、準備工作及下載源碼包

  yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig

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

  2、安裝

  tar zxf varnish-3.0.3.tar.gz

  cd varnish-3.0.3

  ./autogen.sh

  ./configure --prefix=/usr/local/varnish

  make && make install

  3、添加Varnishd進程使用者www,使用者組www,建立/var/vcache目錄,使www使用者有許可權可讀寫

  groupadd www

  useradd www -g www

  mkdir /home/vcache

  chown -R www:www /home/vcache

  chmod -R 750 /home/vcache

  4、編輯/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

  運行sysctl -p 重新按設定檔設定核心參數

  5、啟動Varnishd

  /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500

  參數說明:

  -u 以什麼用運行

  -g 以什麼組運行

  -f varnish設定檔

  -a 綁定IP和連接埠

  -s varnish快取檔案位置與大小

  -w 最小,最大線程和逾時時間

  -T varnish管理連接埠,主要用來清除緩衝

  -p client_http11=on 支援http1.1協議

  -P(大P) /usr/local/varnish/var/varnish.pid 指定其進程碼檔案的位置,實現管理

  6、啟動varnishncsa用來將Varnish訪問日誌寫入記錄檔:

  /usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &

  7、Varnish 緩衝清除

  /usr/local/varnish/bin/varnishadm -T 192.168.1.180:3500 purge "req.http.host ~ www.5013.org$ && req.url ~ /static/image/tp.php"

  說明:

  192.168.1.180:3000 為被清除快取服務器地址

  www.5013.org 為被清除的網域名稱

  /static/image/tp.php 為被清除的url地址清單

  清除所有緩衝

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

  清除image目錄下所有緩衝

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

  8、將加入啟動項

  vi /etc/rc.local

  ulimit -SHn 51200

  /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/varnish.conf -a 0.0.0.0:80 -s file,/home/vcache/varnish_cache.data,100M -w 1024,8192,10 -t 3600 -T 127.0.0.1:3500

  /usr/local/varnish/bin/varnishncsa -n /home/vcache -w /var/log/varnish.log &

  9、殺掉varnishd進程

  pkill varnishd

  10、查看varnishd命中率

  /usr/local/varnish/bin/varnishstat

  11、更新系統時間

  yum install -y ntp

  ntpdate time.nist.gov

  echo "00 01 * * * ntpdate time.nist.gov" 》 /etc/crontab

  附件多主機多網域名稱varnish.conf 配置

  backend blog {

  .host = "198.56.193.190";

  .port = "80";

  }

  backend www {

  .host = "192.168.1.170";

  .port = "80";

  }

  sub vcl_recv {

  if (req.http.host ~ "^(www.)?5013.org$") {

  set req.backend = blog;

  } elsif (req.http.host ~ "^(www.)?(test1.com|test2.com)$") {

  set req.backend = www;

  } else {

  error 404 "Unknown virtual host";

  }

  }

  sub vcl_recv {

  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") {

  /* Non-RFC2616 or CONNECT which is weird. */

  return (pipe);

  }

 #只有GET與HEAD方法才會使用Lookup,使用緩衝。
    if (req.request != "GET" && req.request != "HEAD") {
    /* We only deal with GET and HEAD by default */
    return (pass);
    }
    # if (req.http.Authorization || req.http.Cookie) {
    #     /* Not cacheable by default */
    #     return (pass);
    # }
    #如果請求的是php頁面直接轉寄到後端伺服器
    if (req.url ~ ".(php|cgi)($|?)") {
    return (pass);
    }
    return (lookup);
    }
    sub vcl_pipe {
    return (pipe);
    }
    sub vcl_pass {
    return (pass);
    }
    sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
    hash_data(req.http.host);
    } else {
    hash_data(server.ip);
    }
    return (hash);
    }
    sub vcl_hit {
    return (deliver);
    }
    sub vcl_miss {
    return (fetch);
    }
    sub vcl_fetch {
    if (beresp.ttl <= 0s ||
    beresp.http.Set-Cookie ||
    beresp.http.Vary == "*") {
    /*
    * Mark as "Hit-For-Pass" for the next 2 minutes
    */
    set beresp.ttl = 120 s;
    return (hit_for_pass);
    }
    if (req.url ~ ".(png|gif|jpg)$") {
    unset beresp.http.set-cookie;
    set beresp.ttl = 1h;
    }
    #設定圖片的緩衝TTL為一小時
    return (deliver);
    }
    sub vcl_deliver {
    return (deliver);
    }
    sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    set obj.http.Retry-After = "5";
    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.xid + {"</p>
    <hr>
    <p>Varnish cache server</p>
    </body>
    </html>
    "};
    return (deliver);
    }
    sub vcl_init {
    return (ok);
    }
    sub vcl_fini {
    return (ok);
    }

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。