不卡AV在线|网页在线观看无码高清|亚洲国产亚洲国产|国产伦精品一区二区三区免费视频

學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝

Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝

時間: 春健736 分享

Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝

  Varnish經(jīng)常被用來作反向代理來加速HTTP傳輸,那么Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝呢?下面跟著學(xué)習(xí)啦小編一起來了解一下吧。

  Varnish緩存服務(wù)器在linux系統(tǒng)下安裝方法

  Varnish是一款高性能且開源的反向代理服務(wù)器和http加速器。與傳統(tǒng)的Squid相比,Varnish具有性能更高、速度更快、管理更方便等諸多優(yōu)點。作者Poul-Henning Kamp是FreeBSD的內(nèi)核開發(fā)者之一。Varnish采用全新的軟件體系架構(gòu),和現(xiàn)在的硬件提交配合緊密。在1975年時,儲存媒介只有兩種:內(nèi)存與硬盤。但現(xiàn)在計算 機系統(tǒng)的內(nèi)存除了主存外,還包括了cpu內(nèi)的L1、L2,甚至有L3快取。硬盤上也有自己的快取裝置,因此squid cache自行處理物件替換的架構(gòu)不可能得知這些情況而做到最佳化,但操作系統(tǒng)可以得知這些情況,所以這部份的工作應(yīng)該交給操作系統(tǒng)處理,這就是 Varnish cache設(shè)計架構(gòu)。

  挪威最大的在線報紙 Verdens Gang (http://www.vg.no) 使用3臺Varnish代替了原來的12臺squid,性能居然比以前更好,這是Varnish最成功的應(yīng)用案例。

  Varnish特點:

  1.基于內(nèi)存進行緩存,重啟后數(shù)據(jù)將消失

  2.利用虛擬內(nèi)存方式,I/O性能好

  3.支持設(shè)置0~60秒的精確緩存時間

  4.VCL配置管理比較靈活

  5.32位機器上緩存文件大小最大為2G

  6.具有強大的管理功能,例如top、stat、admin、list等

  7.狀態(tài)機設(shè)置巧妙,結(jié)構(gòu)清晰

  8.利用二叉堆管理緩存文件,可達到積極刪除目的

  Varnish與Squid的對比

  Squid是一個高性能的代理緩存服務(wù)器,它和varnish之間有諸多的異同點,如下:

  相同點:

  都是一個反向代理服務(wù)器

  都是開源軟件

  不同點,也是Varnish的優(yōu)點:

  Varnish的穩(wěn)定性很高,兩者在完成相同負荷的工作時,Squid服務(wù)器發(fā)生故障的幾率要高于Varnish,因為使用Squid要經(jīng)常重啟。

  Varnish訪問速度更快,Varnish采用了“Visual Page Cache”技術(shù),所有緩存數(shù)據(jù)都直接從內(nèi)存讀取,而squid是從硬盤讀取,因而Varnish在訪問速度方面會更快。

  Varnish可以支持更多的并發(fā)連接,因為Varnish的TCP連接釋放要比Squid快。因而在高并發(fā)連接情況下可以支持更多TCP連接。

  Varnish可以通過管理端口,使用正則表達式批量的清除部分緩存,而Squid是做不到的。

  squid屬于是單進程使用單核CPU,但Varnish是通過fork形式打開多進程來做處理,所以是合理的使用所有核來處理相應(yīng)的請求。

  當(dāng)然,與傳統(tǒng)的Squid相比,Varnish也是有缺點的,如下:

  varnish進程一旦掛起、崩潰或者重啟,緩存數(shù)據(jù)都會從內(nèi)存中完全釋放,此時所有請求都會發(fā)送到后端服務(wù)器,在高并發(fā)情況下,會給后端服務(wù)器造成很大壓力。

  在varnish使用中如果單個url的請求通過HA/F5(負載均衡)每次請求不同的varnish服務(wù)器中,被請求varnish服務(wù)器都會被穿透到后端,而且同樣的請求會在多臺服務(wù)器上緩存,也會造成varnish的緩存的資源浪費,也會造成性能下降。

  解決方案:

  綜上所述在訪問量很大的情況下推薦使用varnish的內(nèi)存緩存方式啟動,而且后面需要跟多臺squid服務(wù)器。主要為了防止前面的varnish服務(wù)、服務(wù)器被重啟的情況下,前期肯定會有很多的穿透這樣squid可以擔(dān)當(dāng)?shù)诙觕ache,而且也彌補了varnish緩存在內(nèi)存中重啟都會釋放的問題。

  這樣的問題可以在負載均衡上做url哈希,讓單個url請求固定請求到一臺varnish服務(wù)器上,可以解決該問題。

  varnish的工作流程

  1、進程之間通信

  varnish啟動或有2個進程 master(management)進程和child(worker)進程。master讀入存儲配置命令,進行初始化,然后fork,監(jiān)控child。child則分配線程進行cache工作,child還會做管理線程和生成很多worker線程。

  child進程主線程初始化過程中,將存儲大文件整個加載到內(nèi)存中,如果該文件超出系統(tǒng)的虛擬內(nèi)存,則會減少原來配置mmap大小,然后繼續(xù)加載,這時候創(chuàng)建并初始化空閑存儲結(jié)構(gòu)體,放在存儲管理的struct中,等待分配。

  接著varnish某個負責(zé)接口新http連接的線程開始等待用戶,如果有新的http連接,但是這個線程只負責(zé)接收,然后喚醒等待線程池中的work線程,進行請求處理。

  worker線程讀入uri后,將會查找已有的object,命中直接返回,沒有命中,則會從后端服務(wù)器中取出來,放到緩存中。如果緩存已滿,會根據(jù)LRU算法,釋放舊的object。對于釋放緩存,有一個超時線程會檢測緩存中所有object的生命周期,如果緩存過期(ttl),則刪除,釋放相應(yīng)的存儲內(nèi)存。

  2、配置文件各結(jié)構(gòu)之間通信

  Varnish安裝

  代碼如下:

  wget http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.33.tar.gz

  tar xzf pcre-8.33.tar.gz

  cd pcre-8.33

  ./configure

  make && make install

  cd ../

  varnish-3.0.4報錯如下:

  varnishadm.c:48:33: error: editline/readline.h: No such file or directory

  varnishadm.c: In function 'cli_write':

  varnishadm.c:76: warning: implicit declaration of function 'rl_callback_handler_remove'

  varnishadm.c:76: warning: nested extern declaration of 'rl_callback_handler_remove'

  varnishadm.c: In function 'send_line':

  varnishadm.c:179: warning: implicit declaration of function 'add_history'

  varnishadm.c:179: warning: nested extern declaration of 'add_history'

  varnishadm.c: In function 'varnishadm_completion':

  varnishadm.c:216: warning: implicit declaration of function 'rl_completion_matches'

  varnishadm.c:216: warning: nested extern declaration of 'rl_completion_matches'

  varnishadm.c:216: warning: assignment makes pointer from integer without a cast

  varnishadm.c: In function 'pass':

  varnishadm.c:233: error: 'rl_already_prompted' undeclared (first use in this function)

  varnishadm.c:233: error: (Each undeclared identifier is reported only once

  varnishadm.c:233: error: for each function it appears in.)

  varnishadm.c:235: warning: implicit declaration of function 'rl_callback_handler_install'

  varnishadm.c:235: warning: nested extern declaration of 'rl_callback_handler_install'

  varnishadm.c:239: error: 'rl_attempted_completion_function' undeclared (first use in this function)

  varnishadm.c:300: warning: implicit declaration of function 'rl_forced_update_display'

  varnishadm.c:300: warning: nested extern declaration of 'rl_forced_update_display'

  varnishadm.c:303: warning: implicit declaration of function 'rl_callback_read_char'

  varnishadm.c:303: warning: nested extern declaration of 'rl_callback_read_char'

  make[3]: *** [varnishadm-varnishadm.o] Error 1

  make[3]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin/varnishadm'

  make[2]: *** [all-recursive] Error 1

  make[2]: Leaving directory `/root/lnmp/src/varnish-3.0.4/bin'

  make[1]: *** [all-recursive] Error 1

  make[1]: Leaving directory `/root/lnmp/src/varnish-3.0.4'

  make: *** [all] Error 2

  報錯沒找到解決方法,選varnish-3.0.3

  代碼如下:

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

  tar xzf varnish-3.0.3.tar.gz

  cd varnish-3.0.3

  export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

  ./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking --with-jemalloc

  make && make install

  /usr/bin/install -m 755 ./redhat/varnish.initrc /etc/init.d/varnish

  /usr/bin/install -m 644 ./redhat/varnish.sysconfig /etc/sysconfig/varnish

  /usr/bin/install -m 755 ./redhat/varnish_reload_vcl /usr/local/varnish/bin

  useradd -M -s /sbin/nologin varnish

  代碼如下:

  ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/

  ln -s /usr/local/varnish/bin/varnish_reload_vcl /usr/bin/

  ln -s /usr/local/varnish/bin/varnishadm /usr/bin/

  代碼如下:

  chkconfig --add varnish

  chkconfig varnish on

  生成varnish管理秘鑰:

  代碼如下:

  uuidgen > /usr/local/varnish/etc/varnish/secret

  chmod 644 /usr/local/varnish/etc/varnish/secret

  修改varnish啟動配置:

  代碼如下:

  sed -i "s@^VARNISH_VCL_CONF=/etc/varnish/default.vcl@#VARNISH_VCL_CONF=/etc/varnish/default.vcl\nVARNISH_VCL_CONF=/usr/local/varnish/etc/varnish/linuxeye.vcl@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_LISTEN_PORT=6081@#VARNISH_LISTEN_PORT=6081\nVARNISH_LISTEN_PORT=80@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_SECRET_FILE=/etc/varnish/secret@#VARNISH_SECRET_FILE=/etc/varnish/secret\nVARNISH_SECRET_FILE=/usr/local/varnish/etc/varnish/secret@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin@#VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin\nVARNISH_STORAGE_FILE=/usr/local/varnish/var/varnish_storage.bin@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_STORAGE_SIZE.*@VARNISH_STORAGE_SIZE=150M@" /etc/sysconfig/varnish

  sed -i "s@^VARNISH_STORAGE=.*@VARNISH_STORAGE=\"malloc,${VARNISH_STORAGE_SIZE}\"@" /etc/sysconfig/varnish

  假設(shè)你的服務(wù)器擁有多顆邏輯處理器,還可以做以下的設(shè)置:

  /etc/sysconfig/varnish 里面還可以添加自定義的參數(shù),用”-p 參數(shù)“的方式添加,如:

  DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \

  -f ${VARNISH_VCL_CONF} \

  -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \

  -t ${VARNISH_TTL} \

  -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \

  -u varnish -g varnish \

  -S ${VARNISH_SECRET_FILE} \

  -s ${VARNISH_STORAGE} \

  -p thread_pools=2" #這里為添加項

  Varnish啟動后進入后臺運行,同時返回命令行狀態(tài)。需要注意的是,Varnish運行時會同時啟動兩個進程,一個主進程,一個是子進程,如果子進程出現(xiàn)問題,主進程將重新生成一個子進程。

  VCL配置

  代碼如下:

  /usr/local/varnish/etc/varnish/linuxeye.vcl

  #通過backend定義了一個名稱為webserver的后端主機,“.host”指定后端主機的IP地址或者域名,“.port”指定后端主機的服務(wù)端口。

  backend webserver {

  .host = "127.0.0.1";

  .port = "8080";

  }

  #調(diào)用vcl_recv開始

  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;

  }

  }

  #如果請求的類型不是GET、HEAD、PUT、POST、TRACE、OPTIONS、DELETE時,進入pipe模式。注意這里是“&&”的關(guān)系

  if (req.request != "GET" &&

  req.request != "HEAD" &&

  req.request != "PUT" &&

  req.request != "POST" &&

  req.request != "TRACE" &&

  req.request != "OPTIONS" &&

  req.request != "DELETE") {

  return (pipe);

  }

  #如果請求的類型不是GET與HEAD,則進入pass模式

  if (req.request != "GET" && req.request != "HEAD") {

  return (pass);

  }

  if (req.http.Authorization || req.http.Cookie) {

  return (pass);

  } <strong></strong>#對linuxeye.com域名進行緩存加速,這是個泛域名的概念,也就是所有以linuxeye.com結(jié)尾的域名都進行緩存

  if (req.http.host ~ "^(.*).linuxeye.com") {

  set req.backend = webserver;

  }

  #對以.jsp、.do、php結(jié)尾以及帶有?的URL時,直接從后端服務(wù)器讀取內(nèi)容

  if (req.url ~ "\.(jsp|do|php)($|\?)") {

  return (pass);

  } else {

  return (lookup);

  }

  }</p> <p>sub vcl_pipe {

  return (pipe);

  }</p> <p>sub vcl_pass {

  return (pass);

  }</p> <p>sub vcl_hash {

  hash_data(req.url);

  if (req.http.host) {

  hash_data(req.http.host);

  } else {

  hash_data(server.ip);

  }

  return (hash);

  }</p> <p>sub vcl_hit {

  return (deliver);

  }</p> <p>sub vcl_miss {

  return (fetch);

  }

  代碼如下:

  #對于請求類型是GET,并且請求的URL中包含upload,那么就進行緩存,緩存的時間是300秒,即5分鐘

  sub vcl_fetch {

  if (req.request == "GET" && req.url ~ "^/upload(.*)$") {

  set beresp.ttl = 300s;

  }</p> <p> if (req.request == "GET" && req.url ~ "\.(png|gif|jpg|jpeg|bmp|swf|css|js|html|htm|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|txt)$") {

  unset beresp.http.set-cookie;

  set beresp.ttl = 30d;

  }

  return (deliver);

  }

  代碼如下:

  #下面是添加一個Header標(biāo)識,以判斷緩存是否命中

  sub vcl_deliver {

  if (obj.hits > 0) {

  set resp.http.X-Cache = "HIT from demo.linuxeye.com";

  } else {

  set resp.http.X-Cache = "MISS from demo.linuxeye.com";

  }

  return (deliver);

  }

  代碼如下:

  #使用vcl_error可以定制一個錯誤頁面

  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);

  }</p> <p>sub vcl_init {

  return (ok);

  }</p> <p>sub vcl_fini {

  return (ok);

  }

  檢查VCL配置是否正確:

  代碼如下:

  service varnish configtest

  或

  代碼如下:

  varnishd -C -f /usr/local/varnish/etc/varnish/linuxeye.vcl

  啟動varnish:

  代碼如下:

  service varnish start

  查看varnish狀態(tài):

  代碼如下:

  service varnish status

  動態(tài)加載VCL配置:

  代碼如下:

  service varnish reload

  停止varnish:

  代碼如下:

  service varnish stop

  查看當(dāng)前varnish監(jiān)聽的80端口:

  代碼如下:

  # netstat -tpln | grep :80

  tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15249/varnishd

  tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 19468/nginx

  tcp 0 0 :::80 :::* LISTEN 15249/varnishd

  查看varnish進程:

  代碼如下:

  # ps -ef | grep varnishd | grep -v grep

  root 15248 1 0 11:47 ? 00:00:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /usr/local/varnish/etc/varnish/linuxeye.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish -S /usr/local/varnish/etc/varnish/secret -s malloc,150M

  varnish 15249 15248 0 11:47 ? 00:00:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /usr/local/varnish/etc/varnish/linuxeye.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish -S /usr/local/varnish/etc/varnish/secret -s malloc,150M

  Varnish訪問日志

  varnishncsa可以使用NCSA通用日志格式(NCSA Common Log Format)將HTTP請求記錄到日志文件.

  代碼如下:

  /usr/bin/install -m 755 ./redhat/varnishncsa.initrc /etc/init.d/varnishncsa

  chmod +x /etc/init.d/varnishncsa

  chkconfig varnishncsa on

  mkdir -p /usr/local/varnish/logs

  編輯varnishncsa啟動配置

  代碼如下:

  ln -s /usr/local/varnish/bin/varnishncsa /usr/bin

  sed -i 's@^logfile.*@logfile="/usr/local/varnish/logs/varnishncsa.log"@' /etc/init.d/varnishncsa

  啟動varnishncsa:

  代碼如下:

  service varnishncsa start

  使用logrotate輪詢?nèi)罩疚募?每天輪詢):

  代碼如下:

  cat > /etc/logrotate.d/varnish << EOF

  /usr/local/varnish/logs/varnishncsa.log {

  daily

  rotate 5

  missingok

  dateext

  compress

  notifempty

  sharedscripts

  postrotate

  [ -e /var/run/varnishncsa.pid ] && kill -USR1 \`cat /var/run/varnishncsa.pid\`

  endscript

  }

  EOF

  日志輪詢debug測試:

  代碼如下:

  logrotate -df /etc/logrotate.d/varnish

看過“ Varnish緩存服務(wù)器在linux系統(tǒng)下怎么安裝 ”的人還看了:

1.Linux系統(tǒng)常用命令合集

2.Linux操作系統(tǒng)

3.Linux系統(tǒng)最常使用的命令有哪些

4.初學(xué)者學(xué)習(xí)linux操作系統(tǒng),用哪個版本比較好

5.Linux操作系統(tǒng)基礎(chǔ)知識

644953