varnish實現Web緩存html
1、varnish簡介web
Varnish 的做者Poul-Henning Kamp是FreeBSD的內核開發者之一,他認爲如今的計算機比起1975年已經複雜許多。在1975年時,儲存媒介只有兩種:內存與硬盤。但如今計算 機系統的內存除了主存外,還包括了CPU內的L1、L2,甚至有L3快取。硬盤上也有本身的快取裝置,所以Squid Cache自行處理物件替換的架構不可能得知這些狀況而作到最佳化,但操做系統能夠得知這些狀況,因此這部份的工做應該交給操做系統處理,這就是 Varnish cache設計架構。後端
varnish項目是2006年發佈的第一個版本0.9.距今已經八年多了,此文檔以前也提過varnish還不穩 定,那是2007年時候編寫的,通過varnish開發團隊和網友們的辛苦耕耘,如今的varnish已經很健壯。不少門戶網站已經部署了 varnish,而且反應都很好,甚至反應比squid還穩定,且效率更高,資源佔用更少。相信在反向代理,web加速方面,varnish已經有足夠能力代替squid。緩存
2、Varnish的工做原理bash
varnish主要運行兩個進程:Management進程和Child進程(也叫Cache進程)。服務器
Management進程主要實現應用新的配置、編譯VCL、監控varnish、初始化varnish以及提供一個命令行接口等。Management進程會每隔幾秒鐘探測一下Child進程以判斷其是否正常運行,若是在指定的時長內未獲得Child進程的迴應,Management將會重啓此Child進程。session
Child進程包含多種類型的線程,常見的如:架構
Acceptor線程:接收新的鏈接請求並響應;併發
Worker線程:child進程會爲每一個會話啓動一個worker線程,所以,在高併發的場景中可能會出現數百個worker線程甚至更多;curl
Expiry線程:從緩存中清理過時內容;
Varnish依賴「工做區(workspace)」以下降線程在申請或修改內存時出現競爭的可能性。在varnish內部有多種不一樣的工做區,其中最關鍵的當屬用於管理會話數據的session工做區。
Varnish處理過程
vcl_recv-->lookup-->vcl_hash-->vcl_hit-->vcl_deliver
vcl_recv-->lookup-->vcl_hash-->vcl_miss-->vcl_fetch-->cache-->vcl_deliver
vcl_recv-->pipe-->vcl_pipe
vcl_recv-->pass-->vcl_pass-->vcl_fetch-->nocache-->vcl_deliver
Receive 狀態:也就是請求處理的入口狀態,根據VCL規則判斷該請求應該是Pass或Pipe,或者進入Lookup(本地查詢)
Lookup 狀態:進入此狀態後,會在hash表中查找數據,若找到,則進入Hit狀態,不然進入miss狀態
Pass 狀態:在此狀態下,會進入後端請求,即進入fetch狀態
Fetch 狀態:在Fetch狀態下,對請求進行後端的獲取,發送請求,得到數據,並進行本地的存儲
Deliver 狀態:將獲取到的數據發送給客戶端,而後完成本次請求
3、Varnish特色
一、是基於內存緩存,重啓後數據將消失
2、利用虛擬內存方式,I/O性能好
3、支持設置0~60秒內的精確緩存時間
4、VCL配置管理比較靈活
5、32位機器上緩存文件大小爲最大2G
6、具備強大的管理功能,例如top,stat,admin,list等
7、狀態機設計巧妙,結構清晰
8、利用二叉堆管理緩存文件,達到積極刪除目的
4、安裝配置varnish
一、環境 OS:CentOS 6.5 Varnish:3.0.7 Varnish IP:192.168.31.178 Web:192.168.31.105
二、安裝varnish [root@varnish-1 ~]# wget https://repo.varnish-cache.org/redhat/varnish-3.0.el6.rpm [root@varnish-1 ~]# yum install varnish -y [root@varnish-1 ~]# sed -i 's@VARNISH_LISTEN_PORT=.*@VARNISH_LISTEN_PORT=80@' /etc/sysconfig/varnish [root@varnish-1 ~]# sed -i 's@VARNISH_STORAGE=.*@VARNISH_STORAGE="malloc,128M"@' /etc/sysconfig/varnish
三、varnish的配置文件內容以下(/etc/varnish/default.vcl) backend default { .host = "192.168.31.105"; .port = "80"; } 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") { return (pipe); } if (req.request != "GET" && req.request != "HEAD") { return (pass); } if (req.http.Authorization || req.http.Cookie) { 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 == "*") { set beresp.ttl = 120 s; return (hit_for_pass); } return (deliver); } sub vcl_deliver { if (obj.hits > 0){ set resp.http.X-Cache = "HIT from " + server.ip; } else { set resp.http.X-Cache = "MISS from " + server.ip; } return (deliver); } sub vcl_init { return (ok); } sub vcl_fini { return (ok); }
5、配置後端Web服務器
[root@web-1 ~]# yum install httpd -y [root@web-1 ~]# echo 192.168.31.105 > /var/www/html/index.html [root@web-1 ~]# service httpd start
6、測試varnish
[root@varnish-1 ~]# curl -I http://192.168.31.178/index.html HTTP/1.1 200 OK Server: Apache/2.2.15 (CentOS) Last-Modified: Sat, 05 Sep 2015 12:49:01 GMT ETag: "c0b6d-f-51eff70ceede6" Content-Type: text/html; charset=UTF-8 Content-Length: 15 Accept-Ranges: bytes Date: Sat, 05 Sep 2015 20:50:35 GMT X-Varnish: 1577077233 Age: 0 Via: 1.1 varnish Connection: keep-alive X-Cache: MISS from 192.168.31.178 #第一次未命中
[root@varnish-1 ~]# curl -I http://192.168.31.178/index.html HTTP/1.1 200 OK Server: Apache/2.2.15 (CentOS) Last-Modified: Sat, 05 Sep 2015 12:49:01 GMT ETag: "c0b6d-f-51eff70ceede6" Content-Type: text/html; charset=UTF-8 Content-Length: 15 Accept-Ranges: bytes Date: Sat, 05 Sep 2015 20:51:35 GMT X-Varnish: 1577077234 1577077233 Age: 60 Via: 1.1 varnish Connection: keep-alive X-Cache: HIT from 192.168.31.178 #第二次以及第三次都命中了