如何在 Rocky Linux 8 上为 Nginx 安装 Varnish 7
在此页
- 先决条件
- 安装 Varnish 7
- 管理 Varnish 服务
- 配置清漆
- 配置 Nginx 以与 Varnish 一起工作
- 验证清漆缓存
- 结论
Varnish 缓存是一种免费的开源缓存加速器,用于通过在内存中缓存内容来加速您的网站。它速度极快并减少了页面加载时间,这有助于您的 SERP 并改善用户体验。许多组织都在使用它,包括 Facebook、Twitter 和维基百科。 Varnish 位于 Nginx 和客户端之间,接收来自客户端的请求并将它们转发到后端。这将提高网络服务器的性能,因为 Varnish 将从内存中提供内容。
在本教程中,我将向您展示如何在 Rocky Linux 8 上的 Nginx 前面安装 Varnish 7。
先决条件
- 一台运行 Rocky Linux 8 的服务器。
- 在服务器上配置了根密码。
安装清漆 7
在开始之前,您需要从系统中禁用现有的清漆模块。您可以通过运行以下命令来禁用它:
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 服务并使其在系统重启时启动:
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 侦听端口 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 已安装并配置了 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 来加速您的网站性能。如果您有任何问题,请随时问我。