亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

使用Varnish代替Squid做網站緩存加速器的詳細解

系統 1986 0

Varnish是一款高性能的開源HTTP加速器,挪威最大的在線報紙 Verdens Gang (vg.no) 使用3臺Varnish代替了原來的12臺squid,性能比以前更好。

   Varnish的作者Poul-Henning Kamp是FreeBSD的內核開發者之一,他認為現在的計算機比起1975年已經復雜許多。在1975年時,儲存媒介只有兩種:內存與硬盤。但現在計算 機系統的內存除了主存外,還包括了cpu內的L1、L2,甚至有L3快取。硬盤上也有自己的快取裝置,因此squid cache自行處理物件替換的架構不可能得知這些情況而做到最佳化,但操作系統可以得知這些情況,所以這部份的工作應該交給操作系統處理,這就是 Varnish cache設計架構。

  Varnish可以在FreeBSD 6.0和Linux 2.6內核上運行。

  1、編譯安裝varnish HTTP加速器:

引用
wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.1.tar.gz
tar zxvf varnish-1.1.1.tar.gz
cd varnish-1.1.1
./configure --prefix=/usr/local/varnish
make && make install


  2、簡單啟動varnish守護進程,用本機80端口去反向代理加速127.0.0.1:81上的Apache服務器:

引用
/usr/local/varnish/sbin/varnishd -a :8080 -b 127.0.0.1:81 -p thread_pool_max=1500 -p thread_pools=5 -p listen_depth=512 -p client_http11=on -w 1,10000,120


  Varnish官方網站: http://www.varnish-cache.org/

  另有一份PDF文檔,說明Varnish原理的: http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2163384

  我測試了一下,在同等配置環境下,Varnish的性能確實要超過Squid,穩定性也不錯,值得繼續去深入研究。

今天寫的這篇關于Varnish的文章,已經是一篇可以完全替代Squid做網站緩存加速器的詳細解決方案了。網上關于Varnish的資料很少,中文資料更是微乎其微,希望本文能夠吸引更多的人研究、使用Varnish。

在我看來,使用Varnish代替Squid的理由有三點:
  1、Varnish采用了“Visual Page Cache”技術,在內存的利用上,Varnish比Squid具有優勢,它避免了Squid頻繁在內存、磁盤中交換文件,性能要比Squid高。
  2、Varnish的穩定性還不錯,我管理的一臺圖片服務器運行Varnish已經有一個月,沒有發生過故障,而進行相同工作的Squid服務器就倒過幾次。
  3、通過Varnish管理端口,可以使用正則表達式快速、批量地清除部分緩存,這一點是Squid不能具備的。

點擊在新窗口中瀏覽此圖片



下面來安裝Varnish網站緩存加速器(Linux系統):
  1、創建www用戶和組,以及Varnish緩存文件存放目錄(/var/vcache):

/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache



  2、創建Varnish日志目錄(/var/logs/):

mkdir -p /var/logs
chmod +w /var/logs
chown -R www:www /var/logs



  3、編譯安裝varnish:

wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.2.tar.gz
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install



  4、創建Varnish配置文件:

vi /usr/local/varnish/vcl.conf


  輸入以下內容:

引用
backend myblogserver {
? ? ? ?set backend.host = "192.168.0.5";
? ? ? ?set backend.port = "80";
}

acl purge {
? ? ? ?"localhost";
? ? ? ?"127.0.0.1";
? ? ? ?"192.168.1.0"/24;
}

sub vcl_recv {
? ? ? ?if (req.request == "PURGE") {
? ? ? ? ? ? ? ?if (!client.ip ~ purge) {
? ? ? ? ? ? ? ? ? ? ? ?error 405 "Not allowed.";
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?lookup;
? ? ? ?}

? ? ? ?if (req.http.host ~ "^blog.s135.com") {
? ? ? ? ? ? ? ?set req.backend = myblogserver;
? ? ? ? ? ? ? ?if (req.request != "GET" && req.request != "HEAD") {
? ? ? ? ? ? ? ? ? ? ? ?pipe;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else {
? ? ? ? ? ? ? ? ? ? ? ?lookup;
? ? ? ? ? ? ? ?}
? ? ? ?}
? ? ? ?else {
? ? ? ? ? ? ? ?error 404 "Zhang Yan Cache Server";
? ? ? ? ? ? ? ?lookup;
? ? ? ?}
}

sub vcl_hit {
? ? ? ?if (req.request == "PURGE") {
? ? ? ? ? ? ? ?set obj.ttl = 0s;
? ? ? ? ? ? ? ?error 200 "Purged.";
? ? ? ?}
}

sub vcl_miss {
? ? ? ?if (req.request == "PURGE") {
? ? ? ? ? ? ? ?error 404 "Not in cache.";
? ? ? ?}
}

sub vcl_fetch {
? ? ? ?if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
? ? ? ? ? ? ? ?set obj.ttl = 3600s;
? ? ? ?}
? ? ? ?else {
? ? ? ? ? ? ? ?set obj.ttl = 30d;
? ? ? ?}
}


  這里,我對這段配置文件解釋一下:
  (1)、Varnish通過反向代理請求后端IP為192.168.0.5,端口為80的web服務器;
  (2)、Varnish允許localhost、127.0.0.1、192.168.0.***三個來源IP通過PURGE方法清除緩存;
  (3)、Varnish對域名為blog.s135.com的請求進行處理,非blog.s135.com域名的請求則返回“Zhang Yan Cache Server”;
  (4)、Varnish對HTTP協議中的GET、HEAD請求進行緩存,對POST請求透過,讓其直接訪問后端Web服務器。之所以這樣配置,是因為POST請求一般是發送數據給服務器的,需要服務器接收、處理,所以不緩存;
  (5)、Varnish對以.txt和.js結尾的URL緩存時間設置1小時,對其他的URL緩存時間設置為30天。

  5、啟動Varnish

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



  6、啟動varnishncsa用來將Varnish訪問日志寫入日志文件:

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



  7、配置開機自動啟動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 &



  8、優化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:
  1、查看Varnish服務器連接數與命中率:

/usr/local/varnish/bin/varnishstat


點擊在新窗口中瀏覽此圖片

  2、通過Varnish管理端口進行管理:
  用help看看可以使用哪些Varnish命令:

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help

引用
Available commands:
ping [timestamp]
status
start
stop
stats
vcl.load
vcl.inline
vcl.use
vcl.discard
vcl.list
vcl.show
param.show [-l] []
param.set
help [command]
url.purge
dump.pool



  3、通過Varnish管理端口,使用正則表達式批量清除緩存:
  (1)、例:清除類似 http://blog.s135.com/a/zhangyan.html 的URL地址):

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/


  (2)、例:清除類似 http://blog.s135.com/tech 的URL地址:

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


  (3)、例:清除所有緩存:

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



  4、一個清除Squid緩存的PHP函數(清除Varnish緩存同樣可以使用該函數,無需作任何修改,十分方便):

  1. <?php??
  2. function ?purge( $ip ,? $url )??
  3. {??
  4. ???? $errstr ?=? '' ;??
  5. ???? $errno ?=? '' ;??
  6. ???? $fp ?=? fsockopen ?( $ip ,?80,? $errno ,? $errstr ,?2);??
  7. ???? if ?(! $fp )??
  8. ????{??
  9. ????????? return ?false;??
  10. ????}??
  11. ???? else ??
  12. ????{??
  13. ???????? $out ?=? "PURGE?$url?HTTP/1.1\r\n" ;??
  14. ???????? $out ?.=? "Host:blog.s135.com\r\n" ;??
  15. ???????? $out ?.=? "Connection:?close\r\n\r\n" ;??
  16. ???????? fputs ?( $fp ,? $out );??
  17. ???????? $out ?=? fgets ( $fp ?,?4096);??
  18. ????????fclose?( $fp );??
  19. ???????? return ?true;??
  20. ????}??
  21. }??
  22. ??
  23. purge( "192.168.0.4" ,? "/index.php" );??
  24. ?>??



  附1:Varnish官方網站: http://www.varnish-cache.org/

  附2:2007年12月10日,我寫了一個每天0點運行,按天切割Varnish日志,生成一個壓縮文件,同時刪除上個月舊日志的腳本(/var/logs/cutlog.sh):
  /var/logs/cutlog.sh文件內容如下:

引用
#!/bin/sh
# This file run at 00:00
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mv /var/logs/youvideo.log /var/logs/${date}.log
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
mkdir -p /var/logs/youvideo/
gzip -c /var/logs/${date}.log > /var/logs/youvideo/${date}.log.gz
rm -f /var/logs/${date}.log
rm -f /var/logs/youvideo/$(date -d "-1 month" +"%Y-%m*").log.gz


  設置在每天00:00定時執行:

/usr/bin/crontab -e

  或者  

vi /var/spool/cron/root

  輸入以下內容:

引用
0 0 * * * /bin/sh /var/logs/cutlog.sh

使用Varnish代替Squid做網站緩存加速器的詳細解決方案


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美白人极品性喷潮 | 欧美性猛交aa一级 | 国产精品999视频 | 久热99 | 欧美成人专区 | 夜夜快播 | 亚洲最大在线 | 欧美成人xxxx | 久久免费观看国产精品 | 天海翼一区二区三区免费 | 亚洲精品一区二区三区 | 中文字幕97| 玖玖在线播放 | 99久久免费国产香蕉麻豆 | 亚洲va精品中文字幕动漫 | 黄色毛片一级 | 天天干夜夜想 | 性xxx69xxx视频在线观看 | 精品国产91久久久久久久a | 国产亚洲欧美日韩在线看片 | 黄色高清在线观看 | 色狠狠狠狠综合影视 | 国产综合久久久久久 | 毛片免费观看视频 | 午夜在线播放 | 国产91久久精品 | 美女久久久久久久久久久 | 国产永久在线观看 | 四虎在线观看网址 | 久久久久久久国产高清 | 日本强日本不卡一 | 日韩午夜小视频 | 九九51精品国产免费看 | 在线你懂得| 久久国产精品成人免费 | 成人亚洲欧美日韩在线观看 | 国产图片综合 | 久久久久久久久久久观看 | 亚洲激情在线观看 | 日本毛片高清免费视频 | 国产精品福利视频手机免费观看 |