网站搜索

如何在 CentOS 7 上为 Nginx 安装 Varnish Cache 5.2


Varnish Cache(也称为Varnish)是一种开源、高性能 HTTP 加速器,旨在加速 Web 服务器的速度。在上一篇文章中,我们解释了如何在 CentOS 7 和 CentOS 8 上为 Apache 设置 Varnish 缓存。

在本文中,我们将向您展示如何在 CentOS 7 中安装和使用 Varnish Cache 作为 Nginx HTTP 服务器的前端。本指南也适用于 RHEL 7

要求

  1. 安装了 Apache 的 CentOS 7
  2. 具有静态 IP 地址的 CentOS 7

步骤1:在CentOS 7上安装Nginx Web服务器

1. 首先使用 YUM 包管理器从默认 CentOS 软件存储库安装 Nginx HTTP 服务器,如下所示。

yum install nginx

2.安装完成后,暂时启动Nginx服务,并使其在系统启动时自动启动。

systemctl start nginx
systemctl enable nginx
systemctl status nginx

3. 现在使用以下命令修改系统防火墙规则以允许端口 80 上的入站数据包。

firewall-cmd --zone=public --permanent --add-port=80/tcp
firewall-cmd --reload
firewall-cmd --zone=public --permanent --add-port=8080/tcp
firewall-cmd --reload

步骤2:在CentOS 7上安装Varnish缓存

4. 现在有最新版本 Varnish Cache 6 的预编译 RPM 包(即撰写本文时的 6.5),因此您需要添加官方的 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_varnish5.repo 的文件,其中包含以下存储库配置。

vi /etc/yum.repos.d/varnishcache_varnish65.repo

重要:确保将下面配置中的 el7 替换为您的 Linux 发行版和版本:

[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 缓存包(不要忘记输入 yyes 接受 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配置文件,它是使用vanish配置语言(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

步骤 3:配置 Nginx 以使用 Varnish 缓存

11. 在此步骤中,您需要配置 Nginx 以与 Varnish Cache 配合使用。默认情况下,Nginx 侦听端口 80,您应该将默认 Nginx 端口更改为 8080,以便它在 Varnish 缓存后面运行。

打开Nginx配置文件/etc/nginx/nginx.conf并找到listen 80行并将其更改为listen 8080,就像在服务器中一样块如下面的屏幕截图所示。

vi /etc/nginx/nginx.conf

注意:对于您想要通过 提供服务的网站,应在所有服务器块配置文件(通常在 /etc/nginx/conf.d/ 下创建)中完成此操作>清漆

12.接下来,打开varnish服务配置文件,找到指定Varnish监听端口的参数ExecStart,将其值从6081更改为到80

systemctl edit --full  varnish

该线应如图所示。

ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

13.接下来,在/etc/varnish/default.vcl配置文件中将Nginx设置为Varnish代理的后端服务器。

vi /etc/varnish/default.vcl 

找到后端部分,并定义主机IP和端口。以下是默认的后端配置,将其设置为指向您的实际内容服务器。

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

14.执行所有必要的配置后,重新启动Nginx HTTPD和Varnish缓存以使上述更改生效。

systemctl daemon-reload
systemctl restart nginx
systemctl restart varnish

步骤 4:在 Nginx 上测试 Varnish 缓存

15.最后,测试 Varnish 缓存是否已启用并使用下面的 cURL 命令与 Nginx 服务一起查看 HTTP 标头。

curl -I http://localhost
样本输出
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Wed, 06 Jan 2021 09:24:18 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
ETag: "53762af0-12e1"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.5)
Accept-Ranges: bytes
Connection: keep-alive

您可以从 Varnish Cache Github 存储库中找到更多信息:https://github.com/varnishcache/varnish-cache

在本教程中,我们解释了如何在 CentOS 7 上为 Nginx HTTP 服务器设置 Varnish 缓存。使用下面的评论表向我们发送任何疑问或额外的想法。