一、簡介
Varnish 是一款高性能且開源的反向代理服務器和 HTTP 加速器,其采用全新的軟件體系機構,和現在的硬件體系緊密配合,與傳統的 Squid 相比,Varnish 具有性能更高、速度更快、管理更加方便等諸多優點。
二、Varnish 與 Squid 的對比
相同點
- 都是一個反向代理服務器。
- 都是開源軟件。
Varnish的優勢
- Varnish 的穩定性很高。兩者在完成相同負荷的工作時,Squid服務器發生故障的幾率要高於Varnish,因為使用Squid要經常重啟。
- Varnish 訪問速度更快。因為采用了“Visual Page Cache”技術,所有緩存數據都直接從內存讀取,而squid是從硬盤讀取,因而Varnish在訪問速度方面會更快。
- Varnish 可以支持更多的並發連接。因為Varnish的TCP連接釋放要比Squid快,因而在高並發連接情況下可以支持更多TCP連接。
- Varnish 可以通過管理端口,使用正則表達式批量的清除部分緩存,而Squid是做不到的。
- Squid屬於是單進程使用單核CPU,但Varnish是通過fork形式打開多進程來做處理,所以可以合理的使用所有核來處理相應的請求。
Varnish的劣勢
- Varnish進程一旦Hang、Crash或者重啟,緩存數據都會從內存中完全釋放,此時所有請求都會發送到后端服務器,在高並發情況下,會給后端服務器造成很大壓力。
- 在Varnish使用中如果單個url的請求通過HA/F5等負載均衡,則每次請求落在不同的varnish服務器中,造成請求都會被穿透到后端;而且同樣的請求在多台服務器上緩存,也會造成varnish的緩存的資源浪費,造成性能下降。
Varnish劣勢的解決方案
- 針對劣勢一:在訪問量很大的情況下推薦使用varnish的內存緩存方式啟動,而且后面需要跟多台squid服務器。主要為了防止前面的varnish服 務、服務器被重啟的情況下,大量請求穿透varnish,這樣squid可以就擔當第二層CACHE,而且也彌補了varnish緩存在內存中重啟都會釋 、放的問題。
- 針對劣勢二:可以在負載均衡上做url哈希,讓單個url請求固定請求到一台varnish服務器上。
三、Varnish 6.2.2 的安裝
Varnish 的官方鏈接:https://varnish-cache.org/
Varnish 的部署文檔:https://varnish-cache.org/docs/index.html#
注意:Centos7默認yum安裝版本為4.0.5,網上文檔支持比較多;最新版本為6.3.0,較4.x老版本變化較大,需要參照官方文檔進行學習
1、Varnish 安裝前的准備
Varnish 安裝環境的准備:
- 主機名:Varnish
- 操作系統:CentOS Linux release 7.6.1810 (Core)
- IP地址:192.168.1.194
[root@varnish ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
[root@varnish ~]# mkdir -p /data/varnish/{etc,log} [root@varnish ~]# ll /data/varnish/ total 0 drwxr-xr-x. 2 varnish varnish 6 Nov 25 05:45 etc drwxr-xr-x. 2 varnish varnish 6 Nov 25 05:45 log
2、Varnish 安裝前的介紹
Varnish 是一個開源軟件,可以選擇安全二進制包,或者從源碼定制編譯安裝。
在相關操作系統上,可以使用系統自帶的包管理器來安裝,常見的用例:
FreeBSD:
varnish 5.x 以前的安裝方式:
源碼安裝:cd /usr/ports/varnish && make install clean
二進制包安裝: pkg_add -r varnish
varnish 5.x 以后的安裝方式:
源碼安裝:cd /usr/ports/www/varnish4 && make install clean
二進制包安裝: pkg_add -r varnish4
CentOS/RedHat:
# 編譯安裝時,所需安裝的依賴包: yum install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
Debian/Ubuntu:
Varnish 已經發布了 Debian 和 Ubuntu 的軟件包,只需要使用一條命令就可以安裝。(注意:這樣安裝可能不是最新的版本)
apt-get install varnish
Other Systems:
建議:最好使用源碼安裝
3、獲取 Varnish 軟件
Varnish 的官網網址:http://varnish-cache.org/
在官網有 Varnish 的最新說明文檔及版本升級記錄,也可以找到 Varnish 的 SourceForge 的下載鏈接。
目前,Varnish 的最新版本是 Varnish 6.3.1,此處,以 安全版本 Varnish 6.2.2 為例。
# 獲取 Varnish 6.2.2 軟件 [root@varnish ~]# mkdir /soft [root@varnish ~]# cd /soft/ [root@varnish soft]# wget http://varnish-cache.org/_downloads/varnish-6.2.2.tgz
4、開始安裝 Varnish
方法1:二進制包的rpm安裝方式
方法2:源碼包的編譯安裝方式
安裝基礎包:
yum install \ make \ autoconf \ automake \ jemalloc-devel \ libedit-devel \ libtool \ ncurses-devel \ pcre-devel \ pkgconfig \ python3-docutils \ python3-sphinx
# 開始解壓安裝 Varnish [root@varnish soft]# tar xzvf varnish-6.2.2.tgz [root@varnish soft]# cd varnish-6.2.2 [root@varnish varnish-6.2.2]# ./configure --prefix=/data/varnish [root@varnish varnish-6.2.2]# make && make install [root@varnish varnish-6.2.2]# ll /data/varnish/ total 0 drwxr-xr-x. 2 root root 136 Nov 25 06:10 bin drwxr-xr-x. 2 root root 6 Nov 25 06:20 etc drwxr-xr-x. 3 root root 21 Nov 25 06:10 include drwxr-xr-x. 4 root root 142 Nov 25 06:10 lib drwxr-xr-x. 2 root root 6 Nov 25 06:20 log drwxr-xr-x. 2 root root 22 Nov 25 06:10 sbin drwxr-xr-x. 6 root root 58 Nov 25 06:10 share drwxr-xr-x. 3 root root 21 Nov 25 06:10 var # 配置環境變量 [root@varnish ~]# echo 'export PATH=$PATH:/data/varnish/sbin:/data/varnish/bin' >> /etc/profile [root@varnish ~]# source /etc/profile [root@varnish ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/data/varnish/sbin:/data/varnish/bin # 查看 Varnish 的版本 [root@varnish varnish-6.2.2]# varnishd -V varnishd (varnish-6.2.2 revision 3ed1506895ecaddb91f658bee11742f0b0b982b5) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2019 Varnish Software AS
# 拷貝配置文件 [root@varnish varnish-6.2.2]# cp /data/varnish/share/doc/varnish/example.vcl /data/varnish/etc/default.vcl
[root@varnish varnish-6.2.2]# cat /data/varnish/etc/default.vcl # # This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/ # and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the # new 4.0 format. vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don't need, # rewriting the request, etc. } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. }