在CentOS 8 / RHEL 8上为Apache / Nginx安装Varnish Cache 6

也称为清漆缓存 HTTP反向代理缓存 主要的Web应用加速器。通常将其放置在诸如Nginx和Apache之类的Web服务器之前,主要任务是缓存这些服务器的内容以提高加载速度。经过测试,Varnish Cache的交付速度可提高300至1000倍,具体取决于架构。

Varnish Cache将网页存储在内存中,因此Web服务器不必多次创建相同的网页。这使得网页的提供速度比应用程序服务器快得多。大大加快您的网站速度。

本教程描述了如何使用Apache httpd / Nginx Web服务器在CentOS 8 / RHEL 8系统上安装和配置Varnish Cache 6 LTS。 Varnish缓存可以安装在云或数据中心中运行的虚拟机上。选择取决于您,取决于使用它的应用程序的运行位置。

步骤1:更新系统

确保您的系统已更新。

sudo dnf -y update

步骤2:在CentOS 8 / RHEL 8上安装Varnish Cache 6

运行命令以在CenOS 8 / RHEL 8 Linux计算机上安装Varnish Cache 6。

sudo dnf install @varnish

按下 ÿ 当提示您继续安装时,请按一个键。

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 Jul 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 <[email protected]>
Vendor      : CentOS
URL         : https://www.varnish-cache.org/
Summary     : High-performance HTTP accelerator
Description :

在CentOS 8 / RHEL 8上安装Varnish的重要说明

  • 主要的清漆配置文件是 /etc/varnish/default.vcl
  • 清漆秘密档案: / etc /清漆/秘密
  • Varnish Cache可执行二进制文件: / usr / sbin /清漆
  • 清漆系统单位文件: /lib/systemd/system/varnish.service

步骤3:在CentOS 8 / RHEL 8上启动并启用Varnish Cache

由于Varnish Cache安装在CentOS 8 / RHEL 8上,因此请启动该服务并将其设置为在启动时启动。

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.

步骤4:为Nginx / Apache Web服务器配置清漆缓存

如前所述,清漆缓存是一个缓存HTTP反向代理
在Web服务器的前面。目标是加快网络速度
服务器。

安装您喜欢的Web服务器–此演示演示了Nginx / Apache HTTPD服务器的安装。

# Apache
$ sudo dnf -y install @httpd

# Nginx
$ sudo dnf -y install @nginx

1.配置Nginx以使用清漆缓存

默认情况下,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 Cache

如果您使用的是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.配置清漆缓存服务器

现在编辑清漆缓存配置文件并设置监听端口 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;
    }
}

了解有关使用多个后端的更多信息。

步骤5:使用Nginx / Apache测试清漆缓存

最后,测试是否启用了清漆缓存并与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标头信息。

再次运行命令,您应该看到类似以下的内容 清漆缓存的响应 (请注意 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.computingforgeeks.com 
HTTP/2 200 
date: Tue, 28 May 2019 09:09:20 GMT
content-type: text/html
set-cookie: __cfduid=de1acba723fc04cdfb58c84af80ac1b401559034559; expires=Wed, 27-May-20 09:09:19 GMT; path=/; domain=.computingforgeeks.com; HttpOnly; Secure
last-modified: Fri, 10 May 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

我在带有Nginx和Apache Web服务器的CentOS 8 / RHEL 8上安装并配置了Varnish Cache。有关Varnish配置和优化的更多信息,请参见《 Varnish入门》和《 Varnish用户指南》页面。

查看其他相关指南。

在CentOS 7上安装清漆缓存

如何在CentOS 8 / RHEL 8上安装和配置Memcached

如何使用Rsync和Nginx创建CentOS 8本地存储库镜像

Sidebar