Varnish Cache(通常稱為Varnish),是一個開源、流行的反向代理HTTP加速器,旨在加快網絡伺服器的速度。它被設計用於高度使用的API端點,也適用於提供大量內容並經歷高流量的動態網站。
它基本上有助於降低CPU負載;支持網絡伺服器的負載平衡,並通過將快取存儲在RAM中使網頁瀏覽器能夠快速加載網站。許多大公司使用它,包括Facebook、Twitter和Wikipedia等等。
需求
在本文中,我將解釋如何在CentOS 7(也適用於RHEL 7)上安裝和使用Varnish Cache 6.5作為Apache網絡伺服器的前端。
步驟1:在CentOS 7上安裝Apache網絡伺服器
1.首先從默認的CentOS軟件庫使用YUM套件管理器安裝Apache HTTP伺服器如下。
# yum install httpd

2.一旦安裝了Apache,暫時啟動它並啟用它以便在系統啟動時自動啟動。
# systemctl start httpd # systemctl enable httpd # systemctl status httpd

3. 接下來更新系統防火牆規則,以允許通過以下命令在端口80上進入的封包。
# firewall-cmd --zone=public --permanent --add-service=http # firewall-cmd --reload

步驟2:在 CentOS 7 上安裝 Varnish Cache
4. 現在有最新版本的Varnish Cache 6(即在撰寫時的6.5)的預編譯 RPM 套件,因此您需要添加官方Varnish Cache存儲庫。
在此之前,您需要啟用 EPEL 存儲庫以安裝幾個依賴套件,如下所示。
# yum install -y epel-release
5. 接下來,安裝pygpgme,一個用於處理 GPG 簽名的套件,以及yum-utils,一組有用的實用程序,以各種方式擴展 yum 的本地功能。
# yum install pygpgme yum-utils
6. 現在創建一個名為/etc/yum.repos.d/varnishcache_varnish65.repo的文件,其中包含以下存儲庫配置。
# vi /etc/yum.repos.d/varnishcache_varnish65.repo
重要:請確保在下面的配置中用您的 Linux 發行版和版本替換el
和7
:
[varnishcache_varnish65] name=varnishcache_varnish65 baseurl=https://packagecloud.io/varnishcache/varnish65/el/7/$basearch repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish65/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300 [varnishcache_varnish65-source] name=varnishcache_varnish65-source baseurl=https://packagecloud.io/varnishcache/varnish65/el/7/SRPMS repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish65/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300
7. 現在運行以下命令來更新您的本地 yum 快取並安裝 varnish cache 套件(安裝套件時不要忘記通過輸入y
或yes
來接受 GPG 金鑰):
# yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish65' # yum install varnish

8.安裝完Varnish Cache後,主要的可執行檔案會被安裝在/usr/sbin/varnishd,而Varnish的設定檔案則位於/etc/varnish/:
- /etc/varnish/default.vcl – 這是主要的Varnish設定檔案,使用Varnish設定語言(VCL)撰寫。
9.現在啟動Varnish服務,並設定它在系統啟動時自動啟動,以確保它正常運行。如下所示:
# systemctl start varnish # systemctl enable varnish # systemctl status varnish

10.您可以通過查看Varnish可執行檔案以及系統上安裝的版本來確認Varnish安裝是否成功。
$ which varnishd $ varnishd -V
範例輸出
varnishd (varnish-6.5.1 revision 1dae23376bb5ea7a6b8e9e4b9ed95cdc9469fb64) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2020 Varnish Software
第三步:配置Apache與Varnish Cache一起工作
11.現在配置Apache與Varnish Cache一起工作。預設情況下,Apache聽取端口80,您需要將預設的HTTPD端口更改為8080,這將確保HTTPD在Varnish緩存之後運行。
您可以使用sed命令來更改端口80為8080,如下所示。
# sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf
注意:同時,您需要更改每個要通過Varnish提供服務的虛擬主機配置的端口。這是我們測試站點的配置(/etc/httpd/conf.d/tecmint.lan.conf)。
<VirtualHost *:8080> DocumentRoot "/var/www/html/tecmint.lan/" ServerName www.tecmint.lan # Other directives here </VirtualHost>
12. 下一步,打開Varnish的systemd配置文件,尋找指定Varnish侦聽端口的ExecStart參數,將其值從6081更改為80,如截图所示。
# systemctl edit --full varnish
完成配置後,應該看起来像这样。
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

13. 下一步,為Varnish代理設定Apache作為後端服務器,在/etc/varnish/default.vcl 配置文件中。
# vi /etc/varnish/default.vcl
找到backend 部分,並定義主機IP和端口。以下是默认的后端配置,將此设置为指向您的实际内容服务器。
backend default { .host = "127.0.0.1"; .port = "8080"; }
如果您的后端服务器在具有地址10.42.1.10 的不同服务器上运行,那么主机参数应指向此IP地址。
backend server1 { .host = "10.42.1.10"; .port = "8080"; }
14. 完成所有必要配置后,重启HTTPD和Varnish缓存以实施上述更改。
# systemctl daemon-reload # systemctl restart httpd # systemctl restart varnish
步骤4:在Apache上测试Varnish缓存
15. 最后,使用以下HTTPD 服务的cURL命令 测试Varnish是否已启用并正常工作,该命令可用于查看HTTP头。
# curl -I http://localhost
示例输出
HTTP/1.1 200 OK Date: Wed, 06 Jan 2021 08:36:07 GMT Server: Apache/2.4.6 (CentOS) Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT ETag: "1321-5058a1e728280" Accept-Ranges: bytes Content-Length: 4897 Content-Type: text/html; charset=UTF-8 X-Varnish: 131085 Age: 0 Via: 1.1 varnish (Varnish/6.5) Connection: keep-alive
有关更多信息,请查看Varnish缓存的Github仓库:https://github.com/varnishcache/varnish-cache
在這個教程中,我們解釋了如何在CentOS 7上為Apache HTTP伺服器設置Varnish Cache 6.5代理。如果您有任何疑問或額外的想法要分享,請使用下面的反饋表格回覆給我們。
Source:
https://www.tecmint.com/install-varnish-cache-on-centos-7-for-apache/