在CentOS 8/RHEL 8上为Apache/Nginx安装Varnish Cache 6
Varnish Cache(也称为缓存HTTP反向代理)是领先的Web应用程序加速器。它通常位于Web服务器(例如Nginx或者Apache)的前面,其主要工作是缓存这些服务器的内容,以提高加载速度。经过测试,Varnish Cache可以将交付速度提高300倍,具体取决于体系结构。
Varnish Cache将网页存储在内存中,因此Web服务器不必一次又一次地创建相同的网页。这使其能够以比任何应用程序服务器更快的速度提供网页。大大加快了的速度。
在本教程中,将介绍如何使用Apache httpd/Nginx Web服务器在CentOS 8/RHEL 8系统上安装和配置Varnish Cache 6 LTS。 Varnish缓存可以安装在云或者数据中心中运行的虚拟机上。选择取决于运行它的应用程序的运行位置。
更新系统
确保系统已更新。
sudo dnf -y update
在CentOS 8/RHEL 8上安装Varnish Cache 6
通过运行以下命令在CentOS 8/RHEL 8 Linux计算机上安装Varnish Cache 6.
sudo dnf install @varnish
当提示我们继续安装时,请按y键。
Dependencies resolved. =================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================== Installing group/module packages: varnish x86_64 6.0.2-1.module_el8.0.0+19+b2cdb875 AppStream 2.6 M varnish-modules x86_64 0.15.0-4.module_el8.0.0+19+b2cdb875 AppStream 83 k Installing dependencies: annobin x86_64 8.64-1.el8 AppStream 187 k cpp x86_64 8.2.1-3.5.el8 AppStream 10 M dwz x86_64 0.12-9.el8 AppStream 109 k efi-srpm-macros noarch 3-2.el8 AppStream 22 k gcc x86_64 8.2.1-3.5.el8 AppStream 23 M ghc-srpm-macros noarch 1.4.2-7.el8 AppStream 9.3 k go-srpm-macros noarch 2-16.el8 AppStream 14 k isl x86_64 0.16.1-6.el8 AppStream 841 k ocaml-srpm-macros noarch 5-4.el8 AppStream 9.4 k openblas-srpm-macros noarch 2-2.el8 AppStream 7.9 k perl-srpm-macros noarch 1-25.el8 AppStream 11 k python-srpm-macros noarch 3-37.el8 AppStream 14 k python3-rpm-macros noarch 3-37.el8 AppStream 13 k qt5-srpm-macros noarch 5.11.1-2.el8 AppStream 11 k redhat-rpm-config noarch 116-1.el8.0.1 AppStream 82 k rust-srpm-macros noarch 5-2.el8 AppStream 9.2 k glibc-devel x86_64 2.28-42.el8.1 BaseOS 1.0 M glibc-headers x86_64 2.28-42.el8.1 BaseOS 465 k kernel-headers x86_64 4.18.0-80.11.2.el8_0 BaseOS 1.6 M libxcrypt-devel x86_64 4.1.1-4.el8 BaseOS 25 k Installing module profiles: varnish/common Enabling module streams: varnish 6 Transaction Summary =================================================================================================================================================== Install 22 Packages Total download size: 41 M Installed size: 107 M Is this ok [y/N]: y
可以使用以下方法检查有关已安装软件包的更多信息:
$rpm -qi varnish Name : varnish Version : 6.0.2 Release : 1.module_el8.0.0+19+b2cdb875 Architecture: x86_64 Install Date: Mon 04 Nov 2019 10:08:22 PM EAT Group : System Environment/Daemons Size : 9467384 License : BSD Signature : RSA/SHA256, Wed 31 May 2019 12:16:17 AM EAT, Key ID 05b555b38483c65d Source RPM : varnish-6.0.2-1.module_el8.0.0+19+b2cdb875.src.rpm Build Date : Sat 01 Jun 2019 10:15:53 PM EAT Build Host : x86-02.mbox.centos.org Relocations : (not relocatable) Packager : CentOS Buildsys <theitroad@localhost> Vendor : CentOS URL : https://www.varnish-cache.org/ Summary : High-performance HTTP accelerator Description :
有关在CentOS 8/RHEL 8上安装Varnish的主要说明,主要的Varnish配置文件是/etc/varnish/default.vclVarnish秘密文件:/etc/varnish/secretVarnish高速缓存可执行二进制文件:/usr/sbin/varnishdVarnish系统单元文件:/lib/systemd/system/varnish.service
在CentOS 8/RHEL 8上启动并启用Varnish Cache
现在我们已经在CentOS 8/RHEL 8上安装了Varnish Cache,现在启动服务并将其设置为在启动时启动。
sudo systemctl enable --now varnish
确认服务状态:
$systemctl status varnish ● varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-11-04 22:12:12 EAT; 23s ago Process: 4975 ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS) Main PID: 4976 (varnishd) Tasks: 217 Memory: 98.3M CGroup: /system.slice/varnish.service ├─4976 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m └─4986 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m Nov 04 22:12:11 centos8.novalocal systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator... Nov 04 22:12:11 centos8.novalocal varnishd[4975]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91 Nov 04 22:12:11 centos8.novalocal varnishd[4975]: Debug: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Nov 04 22:12:11 centos8.novalocal varnishd[4976]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91 Nov 04 22:12:11 centos8.novalocal varnishd[4976]: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Nov 04 22:12:11 centos8.novalocal varnishd[4975]: Debug: Child (4986) Started Nov 04 22:12:11 centos8.novalocal varnishd[4976]: Child (4986) Started Nov 04 22:12:12 centos8.novalocal varnishd[4976]: Child (4986) said Child starts Nov 04 22:12:12 centos8.novalocal systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
为Nginx/Apache Web服务器配置Varnish缓存
前面我们说过Varnish Cache是一个缓存HTTP反向代理
它位于Web服务器的前面。目标是加快网络速度
服务器。
安装我们喜欢的Web服务器此演示演示了Nginx/Apache HTTPD服务器的安装。
# Apache $sudo dnf -y install @httpd # Nginx $sudo dnf -y install @nginx
1.配置Nginx以使用Varnish缓存
默认情况下,Nginx侦听TCP端口80,我们需要将侦听端口更改为8080。Varnish Cache将使用端口80。
$sudo vi /etc/nginx/nginx.conf ..... server { listen 8080 default_server; listen [::]:8080 default_server; .... }
如果使用虚拟主机功能,请编辑相关的配置文件,例如
$sudo vi /etc/nginx/conf.d/mysite.conf
然后重新启动Nginx。
$sudo systemctl restart nginx
确认设置。
$ss -tunelp | grep 8080 tcp LISTEN 0 128 0.0.0.0:8080 0.0.0.0:* ino:106078 sk:11 <-> tcp LISTEN 0 128 [::]:8080 [::]:* ino:106079 sk:15 v6only:1 <->
2.配置Apache以使用Varnish缓存
如果我们使用的是Apache Web服务器,请将侦听端口设置为8080。
$sudo vi /etc/httpd/conf/httpd.conf ... Listen 8080
也可以使用单个sed命令。
sudo sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf
更改后重新启动httpd服务。
sudo systemctl restart httpd
3.配置Varnish缓存服务器
现在编辑Varnish Cache配置文件,并将侦听端口设置为80。
sudo vi /etc/systemd/system/multi-user.target.wants/varnish.service
编辑以ExecStart开头的行,并更改为:
ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
至:
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
然后重新启动varnish systemd服务。
sudo systemctl daemon-reload sudo systemctl restart varnish
确认清漆使用的端口。
$sudo systemctl status varnish ● varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/usr/lib/systemd/system/varnish.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-11-04 22:20:53 EAT; 7s ago Process: 6333 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS) Main PID: 6335 (varnishd) Tasks: 217 Memory: 93.7M CGroup: /system.slice/varnish.service ├─6335 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m └─6345 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m Nov 04 22:20:52 centos8.novalocal systemd[1]: Stopped Varnish Cache, a high-performance HTTP accelerator. Nov 04 22:20:52 centos8.novalocal systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator... Nov 04 22:20:52 centos8.novalocal varnishd[6333]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91 Nov 04 22:20:52 centos8.novalocal varnishd[6333]: Debug: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Nov 04 22:20:52 centos8.novalocal varnishd[6335]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91 Nov 04 22:20:52 centos8.novalocal varnishd[6335]: Platform: Linux,4.18.0-80.7.1.el8_0.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Nov 04 22:20:52 centos8.novalocal varnishd[6333]: Debug: Child (6345) Started Nov 04 22:20:52 centos8.novalocal varnishd[6335]: Child (6345) Started Nov 04 22:20:53 centos8.novalocal varnishd[6335]: Child (6345) said Child starts Nov 04 22:20:53 centos8.novalocal systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
确保将Nginx/Apache配置为Varnish代理的后端服务器。
$sudo vi /etc/varnish/default.vcl ..... # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } .......
注意端口8080在Apache/Nginx Web服务器上配置。
对于多个后端,配置将如下所示:
backend default { .host = "127.0.0.1"; .port = "8080"; } backend java { .host = "127.0.0.1"; .port = "8000"; }
但是我们需要告诉Varnish将差异网址发送到其中
使用vcl_recv。可以说我们的Java应用程序应该处理URL
以/java /开头。
sub vcl_recv { if (req.url ~ "^/java/") { set req.backend_hint = java; } else { set req.backend_hint = default; } }
有关使用多个后端的更多信息。
在Nginx/Apache上测试Varnish缓存
最后,我们使用以下curl命令测试是否启用了Varnish缓存并使用Apache/Nginx服务:
$curl -I http://localhost HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Mon, 04 Nov 2019 19:22:31 GMT Content-Type: text/html Content-Length: 4057 Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT ETag: "5d9bab28-fd9" X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/6.0) Accept-Ranges: bytes Connection: keep-alive
这为我们提供了HTTP标头信息。
如果重新运行该命令,它将显示Varnish缓存的响应(注意" Age"标题):
$curl -I http://localhost HTTP/1.1 200 OK Server: nginx/1.14.1 Date: Mon, 04 Nov 2019 19:22:31 GMT Content-Type: text/html Content-Length: 4057 Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT ETag: "5d9bab28-fd9" X-Varnish: 5 3 Age: 66 Via: 1.1 varnish (Varnish/6.0) Accept-Ranges: bytes Connection: keep-alive
对于具有DNS A记录集的有效域名,它应该具有相同的作用。
$curl -I https://blog.theitroad.local HTTP/2 200 date: Tue, 28 Jan 2019 09:09:20 GMT content-type: text/html set-cookie: __cfduid=de1acba723fc04cdfb58c84af80ac1b401559034559; expires=Wed, 27-May-20 09:09:19 GMT; path=/; domain=.theitroad.local; HttpOnly; Secure last-modified: Fri, 10 Jan 2019 08:08:40 GMT x-varnish: 65557 17 age: 22 via: 1.1 varnish (Varnish/6.0) accept-ranges: bytes expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" server: cloudflare cf-ray: 4ddf16ce9d86cb77-MBA
我们已在CentOS 8/RHEL 8的Nentx和Apache Web服务器上安装并配置了Varnish Cache。