Varnish實現Web站點加速
阿新 • • 發佈:2019-01-06
Varnish 是一款高效能的開源HTTP加速器,挪威最大的線上報紙 Verdens Gang使用3臺Varnish代替了原來的12臺Squid,效能比以前更好.
Varnish 的作者Poul-Henning Kamp是FreeBSD的核心開發者之一,他認為現在的計算機比起1975年已經複雜許多.在1975年時,儲存媒介只有兩種:記憶體與硬碟.但現在計算機系統的記憶體除了主存外,還包括了CPU內的L1、L2,甚至有L3快取.硬碟上也有自己的快取裝置,因此Squid Cache自行處理物件替換的架構不可能得知這些情況而做到最佳化,但作業系統可以得知這些情況,所以這部份的工作應該交給作業系統處理,這就是 Varnish cache設計架構.
◆編譯安裝Varnish◆
1.安裝依賴包
#yum install -y libtool ncurses-devel pcre-devel libxslt libedit python-imaging python-docutils
yum install -y pcre-devel python-docutils libedit-dev*
2.編譯安裝Varnish
wget http://varnish-cache.org/_downloads/varnish-6.0.0.tgz tar -xzvf varnish-6.0.0.tgz cd varnish-6.0.0/ ./configure --prefix=/usr/local/varnish6 make && make install ln -s /usr/local/varnish6/sbin/* /usr/sbin/ ln -s /usr/local/varnish6/bin/* /usr/local/bin/ cp -a /usr/local/varnish6/share/doc/varnish/example.vcl /usr/local/varnish6/default.vcl
◆Varnish實現負載均衡並實現頁面快取◆
1.編輯Varnish主配置檔案,在相應的區域追加寫入以下標★語句
vim /usr/local/varnish/default.vcl 15 # Default backend definition. Set this to point to your content server. 16 backend default { 17 .host = "127.0.0.1"; 18 .port = "8080"; 19 } 20 ★ backend web1 { #均衡web主機1 ★ .host="192.168.1.13"; ★ .port="80"; #指定埠 ★ .probe = { #開啟健康檢查 ★ .url = "/"; #請求的URL路徑 ★ .interval = 5s; #查詢間隔時間 ★ .timeout = 1s; #超時時間 ★ .window = 5; #滑動窗 ★ .threshold = 3; #上次檢查.window數量的多少 ★ } ★ } ★ backend web2 { ★ .host="192.168.1.14"; #均衡web主機2 ★ .port="80"; #指定埠 ★ .probe = { #開啟健康檢查 ★ .url = "/"; #請求的URL路徑 ★ .interval = 5s; #查詢間隔時間 ★ .timeout = 1s; #超時時間 ★ .window = 5; #滑動窗 ★ .threshold = 3; #上次檢查.window數量的多少, ★ } ★ } ★ ★ import directors; #載入directors模組 ★ ★ sub vcl_init { #快取及加速-03單-高效能快取伺服器 ★ ★ new bar = directors.round_robin(); ★ bar.add_backend(web1); ★ bar.add_backend(web2); ★ ★ } ★ ★ sub vcl_recv { ★ ★ set req.backend_hint = bar.backend(); #指定backend ★ ★ }
2.檢查配置檔案,並啟動Varnish
varnishd -C -f /usr/local/varnish6/default.vcl #檢查語法
varnishd -f /usr/local/varnish6/default.vcl #啟動
pkill varnishd #關閉Varnish
varnishlog #檢視Varnish日誌
netstat -anpt | grep varnishd #檢查是否啟動
3.驗證環節
elinks http://127.0.0.1/ #varnish伺服器會根據演算法分配流量