本页内容

  • 先决条件
  • 安装 Varnish 7
  • 管理 Varnish 服务
  • 配置 Varnish
  • 配置 Nginx 与 Varnish 协同工作
  • 验证 Varnish 缓存
  • 结论

Varnish 缓存是一款免费且开源的缓存加速器,它通过将内容缓存在内存中来为您的网站提速。它速度极快,能缩短页面加载时间,这有助于您的 SERPs 并改善用户体验。包括 Facebook、Twitter 和 Wikipedia 在内的许多组织都在使用它。Varnish 位于 Nginx 与客户端之间,负责接收来自客户端的请求并将其转发到后端。这将提升 Web 服务器的性能,因为 Varnish 会直接从内存中提供内容。

在本教程中,我将向您展示如何在 Rocky Linux 8 上于 Nginx 前端安装 Varnish 7。

先决条件

  • 一台运行 Rocky Linux 8 的服务器。
  • 服务器上已配置 root 密码。

安装 Varnish 7

在开始之前,您需要禁用系统中已有的 varnish 模块。您可以运行以下命令来禁用它:

dnf module disable varnish

您应该会看到以下输出:

==============================================================================================================================================
 Package                           Architecture                     Version                           Repository                         Size
==============================================================================================================================================
Disabling modules:
 varnish                                                                                                                                     

Transaction Summary
==============================================================================================================================================

Is this ok [y/N]: y
Complete!

接下来,使用以下命令安装 EPEL 仓库:

dnf install epel-release -y

接下来,使用以下命令将 Varnish Cache 7 仓库添加到您的系统:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish70/script.rpm.sh | bash

添加仓库后,使用以下命令安装 Varnish 7:

dnf install varnish -y

安装成功后,使用以下命令查看 Varnish 的相关信息:

rpm -qi varnish

您应该会看到以下输出:

Name        : varnish
Version     : 7.0.2
Release     : 1.el8
Architecture: x86_64
Install Date: Sunday 13 February 2022 06:24:35 AM UTC
Group       : System Environment/Daemons
Size        : 8907085
License     : BSD
Signature   : (none)
Source RPM  : varnish-7.0.2-1.el8.src.rpm
Build Date  : Wednesday 12 January 2022 02:25:34 PM UTC
Build Host  : 7fc509e75620
Relocations : (not relocatable)
URL         : https://www.varnish-cache.org/
Summary     : High-performance HTTP accelerator
Description :
This is Varnish Cache, a high-performance HTTP accelerator.

管理 Varnish 服务

安装 Varnish 后,使用以下命令启动 Varnish 服务并设置为在系统重启时自动启动:

systemctl start varnish
systemctl enable varnish

您可以使用以下命令查看 Varnish 的状态:

systemctl status varnish

您将得到以下输出:

? varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-02-13 06:24:56 UTC; 6s ago
  Process: 2555 ExecStart=/usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (c>
 Main PID: 2556 (varnishd)
    Tasks: 217
   Memory: 104.5M
   CGroup: /system.slice/varnish.service
           ??2556 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
           ??2567 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m

Feb 13 06:24:55 rockylinux systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Feb 13 06:24:55 rockylinux varnishd[2556]: Version: varnish-7.0.2 revision 9b5f68e19ca0ab60010641e305fd12822f18d42c
Feb 13 06:24:55 rockylinux varnishd[2556]: Platform: Linux,4.18.0-348.12.2.el8_5.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Feb 13 06:24:55 rockylinux varnishd[2556]: Child (2567) Started
Feb 13 06:24:56 rockylinux varnishd[2556]: Child (2567) said Child starts
Feb 13 06:24:56 rockylinux systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

配置 Varnish

默认情况下,Varnish 监听 6081 端口。在这里,我们将使用 Varnish 来接收传入的 HTTP 连接。因此,您需要将 Varnish 配置为监听 80 端口。您可以通过编辑 /usr/lib/systemd/system/varnish.service 文件来实现:

nano /usr/lib/systemd/system/varnish.service

如下所示,将 6081 端口改为 80:

ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,2g

保存并关闭文件,然后重新加载 systemd 守护进程以使更改生效:

systemctl daemon-reload

接下来,重启 Varnish 服务以使更改生效:

systemctl restart varnish

接下来,使用以下命令确认 Varnish 的监听端口:

ss -antpl | grep varnish

您应该在以下输出中看到 Varnish 的监听端口:

LISTEN 0      10         127.0.0.1:46093      0.0.0.0:*    users:(("varnishd",pid=2819,fd=13))                            
LISTEN 0      128          0.0.0.0:80         0.0.0.0:*    users:(("cache-main",pid=2830,fd=6),("varnishd",pid=2819,fd=6))
LISTEN 0      128        127.0.0.1:8443       0.0.0.0:*    users:(("cache-main",pid=2830,fd=9),("varnishd",pid=2819,fd=9))
LISTEN 0      10             [::1]:33963         [::]:*    users:(("varnishd",pid=2819,fd=12))                            
LISTEN 0      128             [::]:80            [::]:*    users:(("cache-main",pid=2830,fd=7),("varnishd",pid=2819,fd=7))
LISTEN 0      128            [::1]:8443          [::]:*    users:(("cache-main",pid=2830,fd=8),("varnishd",pid=2819,fd=8))

配置 Nginx 与 Varnish 协同工作

在本节中,我们将安装 Nginx 并配置它与 Varnish 协同工作。首先,使用以下命令安装 Nginx 软件包:

dnf install nginx -y

安装 Nginx 后,您需要编辑 Nginx 配置文件,并将 Nginx 的默认监听端口从 80 改为 8080。

使用以下命令编辑 Nginx 主配置文件:

nano /etc/nginx/nginx.conf

找到以下几行:

	listen       80 default_server;
        listen       [::]:80 default_server;

并将它们替换为以下几行:

	listen       8080 default_server;
        listen       [::]:8080 default_server;

完成后保存并关闭文件,然后启动 Nginx 服务以使更改生效:

systemctl start nginx

您也可以使用以下命令查看 Nginx 的状态:

systemctl status nginx

您将得到以下输出:

? nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-02-13 06:29:02 UTC; 8s ago
  Process: 5627 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 5626 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 5624 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 5629 (nginx)
    Tasks: 2 (limit: 11412)
   Memory: 6.7M
   CGroup: /system.slice/nginx.service
           ??5629 nginx: master process /usr/sbin/nginx
           ??5630 nginx: worker process

Feb 13 06:29:02 rockylinux systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 13 06:29:02 rockylinux nginx[5626]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 13 06:29:02 rockylinux nginx[5626]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 13 06:29:02 rockylinux systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 13 06:29:02 rockylinux systemd[1]: Started The nginx HTTP and reverse proxy server.

验证 Varnish 缓存

至此,Varnish 已安装并与 Nginx Web 服务器配置完成。现在,是时候测试 Varnish 缓存了。

运行以下命令来验证 Varnish 缓存:

curl -I http://your-server-ip

如果一切正常,您将得到以下输出:

HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sun, 13 Feb 2022 06:30:06 GMT
Content-Type: text/html
Content-Length: 3429
Last-Modified: Thu, 10 Jun 2021 09:09:03 GMT
ETag: "60c1d6af-d65"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/7.0)
Accept-Ranges: bytes
Connection: keep-alive

结论

恭喜!您已在 Rocky Linux 8 上成功安装了搭配 Nginx 的 Varnish。现在您可以在生产环境中部署 Varnish 来提升网站性能。如果您有任何问题,欢迎随时向我提问。