varnish 概述
Varnish 是一款高性能的開源 HTTP 加速器,挪威最大的在線報紙 Verdens Gang (http://www.vg.no)
使用 3 臺 Varnish 代替了原來的 12 臺 squid,性能居然比以前更好。
Varnish 的作者 Poul-Henning Kamp 是 FreeBSD 的內核開發者之一,他認爲現在的計算機比起 1975
年已經複雜許多。在 1975 年時,儲存媒介只有兩種:內存與硬盤。但現在計算 機系統的內存除了主存外, 還包括了 cpu 內的 L1、L2,甚至有 L3 快取。硬盤上也有自己的快取裝置,因此 squid cache 自行處理 物件替換的架構與可能得知這些情況而做到最佳化,但操作系統可以得知這些情況,所以這部份的工作應 該交給操作系統處理,這就是 Varnish cache 設計架構。
監聽端口號: 6081
官方網站:
https://www.varnish-cache.org/
下載:
https://www.varnish-cache.org/releases
varnish 基礎概念詳解
比起 squid 更加輕量級,大致有以下幾個特點:
·可以基於內存緩存,也可以在磁盤上緩存
如果期望內存大小超過幾十個 G,比如圖片服務器,純粹使用內存,性能未必好,這時候可以使用磁盤 進行緩存,或使用 SSD X 2 做 RAID1 避免磁盤損壞,在實現隨機訪問上 ssd 硬盤要比機械硬盤要好的 多,如果必須要緩存在磁盤上還是建議使用 ssd 磁盤
·可以利用虛擬內存方式,IO 性能會非常好 支持設置 0-60 秒 精確緩存時間
·支持 VCL
其配置是通過 vcl 編程語言來完成的。 其配置需要先轉換成 C 代碼,所以使用 vcl 所寫的配置,要先轉 換成 C 語言代碼,因此要依賴於 GCC 臨時的編譯 vcl 配置的,編譯完之後才能運行起來
注: Varnish Configuration Language - VCL(varnish 配置語言-VCL)。
Varnish 使用區域配置語言,這種語言叫做“VCL”(varnish configuration language),在執行 vcl
時,varnish 就把 VCL 轉換成二進制代碼。
VCL 文件被分爲多個子程序,與同的子程序在與同的時間裏執行,比如一個子程序在接到請求時執 行,另一個子程序在接收到後端服務器傳送的文件時執行。
·獨特的日誌存儲及管理機制
日誌既然保存在內存中,日誌可以供多個應用程序所訪問,所以一般查看命中率,當前請求有多少 get post 方法等等,都需使用專用的工具纔可以查看,比如 varnishshtopvarnishlog 等命令工具用來查看 日誌信息
·支持使用 varnish 狀態引擎 通過巧妙的狀態引擎的設計完成與同的引擎對用戶的請求和緩存代理機制進行處理,用配置文件爲狀態
引擎提供狀態法則,完成緩存處理、完成代理處理等等
varnish 緩存數據機制:
VCL 處理流程圖
處理過程大致分爲如下幾個步驟:
(1)Receive 狀態,也就是請求處理的入口狀態,根據 VCL 規則判斷該請求應該是 Pass 或 Pipe,或者 進入 Lookup(本地查詢)。
(2)Lookup 狀態,在緩存中查找用戶請求的對象,如果緩存中沒有其請求的對象,後續操作很可能會將
其請求的對象進行緩存;進入此狀態後,會在 hash 表中查找數據,若找到,則進入 Hit 狀態,否則進入
miss 狀態。
(3)Pass 狀態,在此狀態下,會進入後端請求,即進入 fetch (取)狀態。
(4)Fetch 狀態,在 Fetch 取狀態下,對請求,進行後端的獲取,發送請求,獲得數據,並進行本地的 存儲。
(5)Deliver 提供狀態, 將獲取到的數據發送給客戶端,然後完成本次請求。 注:
pass:繞過緩存,即與從緩存中查詢內容或與將內容存儲至緩存中;
pipe:與對客戶端進行檢查或做出任何操作,而是在客戶端與後端服務器之間建立專用“管道”,
並直接 將數據在二者之間進行傳送;此時,keep-alive 連接中後續傳送的數據也都將通過此管道進行直接傳送,並與會出現在任何日誌中;
流程:
總結: 用戶通過 varnish 加速時,有 4 線路可以獲得數據。
一、安裝 varnish
1、安裝 varnish, 將上傳 varnish 軟件包
[root@silence80 ~]# rpm -ivh varnish-libs-3.0.6-1.el6.x86_64.rpm
[root@silence80 ~]# rpm -ivh varnish-3.0.6-1.el6.x86_64.rpm
或:
rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-3.0.el6.rpm yum install varnish
參數:
--nosignature 想要略過數字簽名的檢查,與檢測合法性。因爲這個 rpm 包與是 redhat 發佈的。
-i install 安裝
vcl 置文件:
[root@silence80 ~]# ls /etc/varnish/default.vcl varnish 主配置文件
[root@silence80 ~]# vim /etc/sysconfig/varnish
啓 varnish 服務:
1)通過腳步啓動
[root@silence80 ~]# /etc/init.d/varnish start
2)手動啓動
[root@silence80 ~]#varnishd -f /etc/varnish/default.vcl -s malloc,256M -T 127.0.0.1:2000 -a 0.0.0.0:80
or
[root@silence80 ~]#varnishd -a 192.168.1.203:80 -f /etc/varnish/default.vcl -T 127.0.0.1:6082 -t 120 -p thread_pool_min=50 -p thread_pool_max=1000 -p thread_pool_timeout=120 -u varnish -g varnish -S /etc/varnish/secret -s malloc,256M
-s 指定緩存方式 malloc 內存 file 硬盤
-s malloc,256M
-s file,/cache/data,1G
-f /etc/varnish/default.vcl 指定配置文件路徑
-T 127.0.0.1:6082 Varnish 管理ip及端口 (telnet 127.0.0.1 6082)
-t 120
-p thread_pool_min=50 -p thread_pool_max=1000 -p thread_pool_timeout=120 線程最小 最大 數量 超時時間
-u varnish -g varnish 運行varnish進程的用戶及組
-S /etc/varnish/secret varnish安全文件
實戰:配置 xuegod63 成爲 varnish 服務器加速 xuegod64 web 服務器
通過 CDN 加速 web 網站 網宿 , 藍訊
智能 DNS, DNS 視圖
實戰:配置 xuegod63 成爲 varnish 服務器
[root@silence80 ~]# vim /etc/varnish/default.vcl
###配置一個後端服務器 改:
7 backend default {
8 .host = "127.0.0.1";
9 .port = "80";
10 }
爲:
backend web1 {
.host = "192.168.1.84";
.port = "80";
}
###查看緩存命中情況 在:
90 # sub vcl_deliver {
91 # return (deliver);
92 # }
追加:
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from xuegod cache";
}
else {
set resp.http.X-Cache = "MISS from xuegod cache";
}
return (deliver);
}
保存退出。
配置 varnish 服務端口
[root@silence80 ~]# vim /etc/sysconfig/varnish
改:
66 VARNISH_LISTEN_PORT=6081
爲:
VARNISH_LISTEN_PORT=80
改:
92 VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE
#不要將數據緩存到硬盤中,否則硬盤會越來越大,IO隊列越來越長
爲:
VARNISH_STORAGE="malloc,1024M" 定義內存緩存
註釋掉:
106 # -S ${VARNISH_SECRET_FILE} \
啓動 varnish 服務器
[root@silence80 ~]# /etc/init.d/varnish start
Starting Varnish Cache: [ OK ]
配置 xuegod64 爲 web 服務器
[root@silence84 ~]# yum install httpd -y
[root@silence84 ~]# echo 192.168.1.84 > /var/www/html/index.html #創建首頁
[root@silence84 ~]# service httpd restart #在 xuegod64 上開啓 web 服務器
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
測試源站點:
在瀏覽器中訪問:http://192.168.1.84/
測試加速:
在瀏覽器中訪問:http://192.168.1.80/
能夠正常訪問,說明代理設置成功。
測試緩存命中: 擴展:curl 命令
curl 是通過 url 語法在命令行下上傳或下載文件的工具軟件,它支持 http,https,ftp,ftps,telnet 等多種 協議,常被用來抓取網頁和監控 Web 服務器狀態。
參數:
-I 只取 http 響應頭的信息,與取網頁內容
例:
[root@silence80 ~]# curl -I 192.168.1.84 #在 xuegod63 上查看 xuegod64 開啓動的 web 服務 器類型
HTTP/1.1 403 Forbidden
Date: Sat, 08 Aug 2015 11:17:49 GMT Server: Apache/2.2.15 (Red Hat) Accept-Ranges: bytes
Content-Length: 3985
Connection: close
Content-Type: text/html; charset=UTF-8
實例 2:測試緩存命中
[root@silence80 ~]# curl -I 192.168.1.80
HTTP/1.1 503 Service Unavailable
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 419
Accept-Ranges: bytes
Date: Sat, 08 Aug 2015 11:12:20 GMT X-Varnish: 1141298100
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS from linuxidc cache #未命中
再次測試:
[root@silence80 ~]# curl -I 192.168.1.80
。。。
X-Cache: MISS from linuxidc cache #還是沒有命中
解決需要開啓 apache 長鏈接功能
[root@silence84 ~]# vim /etc/httpd/conf/httpd.conf
改: 76 KeepAlive Off
爲: KeepAlive On
[root@silence84 ~]# service httpd restart
再次測試:
[root@silence80 ~]# curl -I 192.168.1.80
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 08 Aug 2015 11:21:11 GMT ETag: "25f61-d-51ccaf318dbb9"
Content-Type: text/html; charset=UTF-8
Content-Length: 13
Accept-Ranges: bytes
Date: Sat, 08 Aug 2015 11:28:36 GMT X-Varnish: 1141298120
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from linuxidc cache
[root@silence80 ~]# curl -I 192.168.1.80
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Sat, 08 Aug 2015 11:21:11 GMT ETag: "25f61-d-51ccaf318dbb9"
Content-Type: text/html; charset=UTF-8
Content-Length: 13
Accept-Ranges: bytes
Date: Sat, 08 Aug 2015 11:28:37 GMT X-Varnish: 1141298121 1141298120
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from linuxidc cache #命中
varnish 服務器基本操作:
[root@silence80 ~]# varnishadm ban.url .*$ #清除所有
[root@silence80 ~]# curl -I 192.168.1.80
。。。
X-Cache: MISS from linuxidc cache
[root@silence80 ~]# varnishadm ban.url /index.html #清除 index.html 頁面緩存 測試:
[root@silence80 ~]# curl -I 192.168.1.80/index.html
[root@silence80 ~]# varnishadm ban.url /admin/$ #清除 admin 目錄緩存
實戰 2:使用 varnish 加速多個與同域名站點的 web 服務器 配置 silence84 和 silence81 爲 web 服務器
silence84 之前已經配置成 web 服務器
配置 silence81 爲 web2 服務器
[root@silence81 ~]# yum install httpd -y
[root@silence81 ~]# echo 192.168.1.81 > /var/www/html/index.html
[root@silence81 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.81 silence81.cn
[root@silence81 ~]# service httpd restart
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
配置 silence80 上的 varnish 服務器:
[root@silence80 ~]# vim /etc/varnish/default.vcl
改:
backend web1 {
.host = "192.168.1.64";
.port = "80";
}
爲:
backend web1 {
.host = "192.168.1.64";
.port = "80";
}
backend web2 {
.host = "192.168.1.62";
.port = "80";
}
#當訪問 www.silence.cn 域名時從 web1 上取數據,訪問 bbs.silence.cn 域名時到 web2 取數據,訪 問其他頁面報錯。
[root@silence80 ~]# vim /etc/varnish/default.vcl #在之前的 sub vcl_deliver { 。。。 } 之 前,插入以下內容:
sub vcl_recv {
if (req.http.host ~ "^(www.)?xuegod.cn") {
set req.http.host = "www.xuegod.cn";
set req.backend = web1;
} elsif (req.http.host ~ "^bbs.xuegod.cn") {
set req.backend = web2;
} else { error 404 "xuegod cache";
}
}
sub vcl_deliver {
if (obj.hits > 0){
set resp.http.X-Cache = "HIT from linuxidc cache";
}
else {
set resp.http.X-Cache = "MISS from linuxidc cache";
}
return (deliver);
}
重新加載 varnish 配置文件:
[root@silence81 ~]# service varnish reload Loading vcl from /etc/varnish/default.vcl Current running config name is boot
Using new config name reload_2015-08-08T19:42:11
VCL compiled. available 2 boot
active 0 reload_2015-08-08T19:42:11
Done
測試:網頁訪問 www.xuegod.cn 和 bbs.xuegod.cn 則會顯示與同的內容。
在 silence81 上測試:
[root@silence81 ~]# vim /etc/hosts #添加 hosts 文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.81 silence81.cn
192.168.1.80 www.xuegod.cn
192.168.1.80 bbs.xuegod.cn
[root@silence81 ~]# yum install elinks -y
[root@silence81 ~]# elinks www.xuegod.cn --dump
192.168.1.81
[root@silence81 ~]# elinks bbs.xuegod.cn --dump
192.168.1.84
測試報錯信息:
http://192.168.1.80/
在win下的host標註
訪問www.silence.cn和bbs.silence.cn
可以結合以下某些功能進行緩存代理
在win下給hosts添加語句
緩存一個網站
vim /etc/varnish/default.vcl
#設置varnish向誰取源
在第7行插入:
backend default {
.host = " www.vfast.com.cn";
.port = "80";
}
acl abc{
"127.0.0.1";
"192.168.1.80";
"192.168.1.84";
}
本地緩存 ctrl+F5 刷新
Varnish查看命令:
[root@silence80 ~]# varnishhist #varnish圖表 可以查看哪些數據是走緩存 哪些走源
[root@silence80 ~]# varnishstat #查看varnish狀態
#刪除url的方法 PURGE
if (req.request == "PURGE") {
if (!client.ip ~ abc) {
error 405 "Method not allowed";
}
return (lookup);
}
定義請求方法
#分析用戶請求方法 哪些請求方法使用pipe函數
#以下列出的請求頭使用pass or lookup ,在28行打開以***釋。
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 or head那麼其他請求方法走pass ,在38行打開以***釋。
if (req.request != "GET" && req.request != "HEAD") {
# /* We only deal with GET and HEAD by default */
return (pass);
}
#假如你的請求方法是get or head 那麼請求頭中包含以下字段也不緩存 ,在42行插入。
if (req.url ~ "\.(html|gif|jpg|png|css)$" && req.request == "GET") {
unset req.http.Cookie; #刪除cookie,讓html|gif|jpg|png|css格式可以緩存
}
#Authorization cookie 走lookup,在46行插入。
if (req.http.Authorization || req.http.Cookie) {
# /* Not cacheable by default */
return (pass);
}
return (lookup);
}
手動添加
#如果url是以.jsp或.do結尾,或者url中包含?這從後端拿數據.
if (req.url ~ "\.(jsp|do)($|\?)") {
return (pass);
}
#除了以上列出的不滿足的,剩下的滿足條件的使用lookup操作
return (lookup);
}
# 對pass函數的定義 打開59行的註釋。
sub vcl_pass {
return (pass);
}
# lookup 子函數 ,打開63行的註釋
# 查詢varnish緩存中是否已經緩存了用戶需要的數據
# 緩存數據存在內存 key = value
sub vcl_hash {
hash_data(req.url); #key的實現 hash 的對象是用戶請求的url
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}
修改73行的hit,77行的miss,81行的fetch,94行的vcl_deliver
#hash函數查詢緩存成功後 命中數據 將數據交給vcl_hit
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "purge OK SUCCESS!!!!!";
}
return (deliver); #hit 將數據交給deliver函數
}
#
# hash函數查詢緩存失敗 爲命中 將用戶請求交給vcl_miss
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "purge OK SUCCESS!!!!!";
}
return (fetch); #miss將請求交給fetch函數
}
#
# fetch 取源函數 去後端源服務器取數據
sub vcl_fetch {
##針對不同的內容設置不同的緩存時間
if (req.url ~ "\.(html|css|jss)$") {
set beresp.ttl = 1d;
} elseif (req.url ~ "\.(jpg)$") {
set beresp.ttl = 120s;
} elseif (req.url ~ "\.(gif|png)$") {
set beresp.ttl = 300s;
}
# 將數據交給用戶 ,打上狀態(hit or miss)
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "ABC-HIT";
} else {
set resp.http.X-Cache = "ABC-MISS";
}
return (deliver);
}
刪除圖片(刪除時需要在客戶端刪除,在varnish上刪除不了,要在varnish上添加可刪除的客戶端ip)
除了配置文件的配置之外,還要再64上配置
silence80爲客戶端
[root@silence80 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.64 silence80.cn silence80
192.168.1.63 silence80.cn silence80
192.168.1.63 www.vfast.com.cn
[root@silence80 ~]# curl -I http://www.vfast.com.cn/p_w_picpaths1/0.jpg
HTTP/1.1 200 OK
Server: nginx
Content-Type: p_w_picpath/jpeg
Last-Modified: Mon, 04 May 2015 05:30:14 GMT
ETag: "554703e6-20412"
Content-Length: 132114
Accept-Ranges: bytes
Date: Sat, 09 May 2015 17:27:39 GMT
X-Varnish: 288597595
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: ABC-MISS
[root@silence80 ~]# curl -I http://www.vfast.com.cn/p_w_picpaths1/0.jpg
HTTP/1.1 200 OK
Server: nginx
Content-Type: p_w_picpath/jpeg
Last-Modified: Mon, 04 May 2015 05:30:14 GMT
ETag: "554703e6-20412"
Content-Length: 132114
Accept-Ranges: bytes
Date: Sat, 09 May 2015 17:27:39 GMT
X-Varnish: 288597596 288597595
Age: 1
Via: 1.1 varnish
Connection: keep-alive
X-Cache: ABC-HIT
第一次訪問會是miss,第二次就變成hit
[root@silence80 ~]# curl -X PURGE http://www.vfast.com.cn/p_w_picpaths1/0.jpg
<?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>200 purge OK SUCCESS!!!!!</title>
</head>
<body>
<h1>Error 200 purge OK SUCCESS!!!!!</h1>
<p>purge OK SUCCESS!!!!!</p>
<h3>Guru Meditation:</h3>
<p>XID: 288597597</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
刪除成功