如何在 AlmaLinux 9 上安装带有 Nginx 的 Varnish

Varnish 是一种流行的开源 Web 应用程序加速器,也称为缓存 HTTP 反向代理,用于加速动态 Web 内容交付。它缓存经常访问的内容并直接从内存中提供服务,从而减少应用程序服务器的负载并缩短用户的响应时间。

Varnish 位于客户端和 Web 服务器之间,充当中介。当用户请求网页时,Varnish 会检查该页面是否已在其缓存中。如果是,Varnish 直接从其缓存中提供页面,而无需从 Web 服务器请求它。如果该页面不在缓存中,Varnish 会从 Web 服务器请求它并缓存它以备将来使用。

Varnish 还允许用户配置缓存策略和规则以优化缓存行为,并提供用于监控和分析缓存性能的工具。高流量网站通常使用它来提高其性能和可扩展性。

在本教程中,我们将向您展示如何在 AlmaLinux 9 上安装和Varnish配置Nginx

步骤 1:更新操作系统

更新您的 AlmaLinux 9 操作系统以确保所有现有软件包都是最新的:

# dnf update

此外,安装 EPEL 包管理器以允许Varnish安装依赖项:

# dnf install epel-release 

第二步:安装 Nginx

您可以dnf 通过执行以下命令通过包管理器安装 Nginx。

# dnf install nginx

Nginx 在安装时不会自动启动。您可以通过输入以下命令启动 Nginx 服务并将其配置为在启动时运行:

# systemctl start nginx
# systemctl enable nginx

Nginx 使用 命令验证服务的状态 systemctl status :

# systemctl status nginx

输出:

● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
    Drop-In: /usr/lib/systemd/system/nginx.service.d
             └─php-fpm.conf
     Active: active (running)
   Main PID: 1720 (nginx)
      Tasks: 2 (limit: 5740)
     Memory: 2.3M
        CPU: 28ms
     CGroup: /system.slice/nginx.service
             ├─1720 "nginx: master process /usr/sbin/nginx"
             └─1721 "nginx: worker process"

如果 firewalld 启用考虑允许 HTTP 和 HTTPS 服务:

$ sudo firewall-cmd --permanent --add-service={http,https}
$ sudo firewall-cmd --reload

现在您需要配置 Nginx 以使用 Varnish Cache。

打开 Nginx 配置文件 /etc/nginx/nginx.conf

 # nano /etc/nginx/nginx.conf

搜索  行并将其更改为 8080

.....   
    server {
        listen       8080;
        listen       [::]:8080;
        server_name  _;
        root         /usr/share/nginx/html;
 .....

保存并关闭文件。然后重新启动 Nginx 服务以应用更改:

# systemctl restart nginx

第 3 步:安装 Varnish 7

Varnish首先从包管理器中禁用该服务dnf

# dnf module disable varnish

默认情况下,最新版本的 Varnish 在 AlmaLinux 9 基础存储库中不可用。

因此,您需要运行以下命令将最新的Varnish存储库添加到您的系统中:

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

添加存储库后,使用以下命令安装 Varnish:

# dnf install varnish

使用以下命令检查安装的版本:

# varnishd -V

输出:

varnishd (varnish-7.3.0 revision 84d79120b6d17b11819a663a93160743f293e63f)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2023 Varnish Software

现在启动并启用Varnish(在系统启动时自动启动):

# systemctl start varnish
# systemctl enable 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)
Main PID: 4266 (varnishd)
Tasks: 217
Memory: 113.4M
CPU: 2.074s
CGroup: /system.slice/varnish.service
├─4266 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -f /etc/varnish/default.vcl -P /run/varnish/varnishd.pid -p feature=+http2 -s malloc>
└─4280 /usr/sbin/varnishd -a :6081 -a localhost:8443,PROXY -f /etc/varnish/default.vcl -P /run/varnish/varnishd.pid -p feature=+http2 -s malloc>

第 4 步:配置 Varnish 缓存

默认Varnish监听端口是6081,现在我们需要配置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 \
-f /etc/varnish/default.vcl \
-P %t/%N/varnishd.pid \
-p feature=+http2 \
-s malloc,256m
ExecReload=/usr/sbin/varnishreload
...

修改后重启Varnish服务使修改生效。

# systemctl daemon-reload
# sudo systemctl restart varnish

第 5 步:测试清漆缓存

使用以下命令测试Varnish缓存是否已启用并使用 Nginx 服务curl

# curl -I http://your-IP-address

输出:

HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 06 Apr 2023 11:22:04 GMT
Content-Type: text/html
Content-Length: 4681
Last-Modified: Sat, 09 Oct 2021 17:49:21 GMT
ETag: "6161d621-1249"
X-Varnish: 2
Age: 0
Via: 1.1 192.168.1.150 (Varnish/7.3)
Accept-Ranges: bytes
Connection: keep-alive

这意味着您的 Varnish Cashe 已激活并在您的服务器上运行。

评论和结论

在本教程中,您学习了如何Varnish在 AlmaLinux 9 上安装和配置。

如需更多帮助或有用信息,您可以查看 Varnish 官方网站

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注