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

Varnish Cache(通常简称为Varnish)是一款开源、强大且快速的反向代理HTTP加速器,具有现代化的架构和灵活的配置语言。作为反向代理意味着它是一种软件,您可以部署在您的网络服务器前面(即源服务器或后端),比如Nginx,以接收客户端的HTTP请求并将其转发到源服务器进行处理。然后将来自源服务器的响应发送给客户端。

Varnish充当Nginx客户端之间的中间人,但具有一些性能优势。它的主要目的是通过作为缓存引擎来使您的应用程序加载更快。它接收来自客户端的请求,并将其转发到后端以缓存所请求的内容(将文件和文件片段存储在内存中)。然后,所有对完全相同内容的未来请求都将从缓存中提供。

这使得您的Web应用程序加载更快,并间接提高了您的Web服务器的整体性能,因为Varnish将从内存中提供内容,而不是Nginx从存储磁盘处理文件。

除了缓存之外,Varnish还具有其他几种用途,包括HTTP请求路由器、负载均衡器、Web应用程序防火墙等。

清漆是使用高度可擴展的內置清漆配置語言VCL)配置的,它使您能夠編寫關於如何處理傳入請求的策略。您可以使用它來構建定制的解決方案、規則和模塊。

在本文中,我們將介紹在全新的CentOS 8RHEL 8服務器上安裝Nginx Web 服務器和Varnish Cache 6的步驟。RHEL 8用戶應確保啟用紅帽訂閱

如果要設置完整的LEMP堆棧,而不僅僅是安裝Nginx Web 服務器,請參閱以下指南。

  1. 在CentOS 8上安裝LEMP服務器的方法
  2. 在RHEL 8上安裝LEMP服務器的方法

步驟1:在CentOS/RHEL 8上安裝Nginx Web 服務器

1. CentOS/RHEL 8 發布了最新版本的 Nginx 網頁伺服器軟體,因此我們將使用以下 dnf 指令 從預設庫安裝它。

# dnf update
# dnf install nginx

2. 安裝完成後,您需要使用以下 systemctl 指令 啟動、啟用並驗證狀態。

# systemctl start nginx
# systemctl enable nginx
# systemctl status nginx
Verify Nginx Service Status

3. 如果您有些好奇,您還可以使用以下 ss 指令 檢查執行在預設端口 80 上的 Nginx TCP socket。

# ss -tpln
Check Nginx TCP Port

4. 如果系統正在運行防火牆,請確保更新防火牆規則以允許對網頁伺服器的請求。

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --reload

步驟 2: 在 CentOS/RHEL 8 上安裝 Varnish Cache 6

5. CentOS/RHEL 8 默認提供了一個含有版本 6.0 LTS(長期支援)的 Varnish Cache DNF 模組。

要安裝此模組,執行以下命令。

# dnf module install varnish
Install Varnish Cache in CentOS 8

6.安裝模組完成後,您可以確認系統上安裝的Varnish版本。

# varnishd -V
Check Varnish Version

7.安裝Varnish Cache後,主要的可執行命令安裝在/usr/sbin/varnishd下,而 varnish 配置文件位於/etc/varnish/

文件/etc/varnish/default.vcl是主要的 varnish 配置文件,使用VCL編寫,而/etc/varnish/secret是 varnish 密鑰文件。

8.接下來,啟動Varnish服務,並在系統啟動時啟用自動啟動,確認其運行正常。

# systemctl start varnish
# systemctl enable varnish
# systemctl status varnish
Verify Varnish Cache on CentOS 8

第三步:配置 Nginx 與 Varnish Cache 一起工作

9.在本部分中,我們將展示如何配置Varnish CacheNginx前運行。默認情況下,Nginx 監聽端口80,通常每個服務器塊(或虛擬主機)都配置為監聽此端口。

例如,查看主配置文件(/etc/nginx/nginx.conf)中配置的默認 nginx 服務器塊

# vi /etc/nginx/nginx.conf

請查找以下屏幕截圖中顯示的服務器塊部分。

Nginx Server Block Configuration

10.要在Varnish之前运行Nginx,您应该将默认的Nginx端口从80更改为8080(或您选择的其他端口)。

这应该在所有未来的服务器块配置文件中完成(通常创建在/etc/nginx/conf.d/下),用于您想通过Varnish提供服务的站点或 Web 应用程序。

例如,我们测试站点tecmint.lan的服务器块是/etc/nginx/conf.d/tecmint.lan.conf,并具有以下配置。

server {
        listen       8080;
        server_name  www.tecmint.lan;
        root         /var/www/html/tecmint.lan/;
        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
}
Nginx Server Block Configuration

重要:记得通过在/etc/nginx/nginx.conf文件中注释掉其配置部分来禁用默认服务器块,如下面的截图所示。这样可以让您在服务器上启动运行其他网站/应用程序,否则,Nginx将始终将请求定向到默认服务器块。

Disable Nginx Server Block

11.配置完成后,检查配置文件是否有任何错误,并重新启动Nginx服务以应用最近的更改。

# nginx -t
# systemctl restart nginx
Check Nginx Configuration Syntax

12.接下来,为了接收来自客户端的HTTP请求,我们需要配置Varnish以在端口80上运行。与早期版本的Varnish Cache中更改了Varnish环境文件(现已弃用)的方式不同,在版本6.0及以上。

我们需要在Varnish服务文件中进行所需的更改。运行以下命令以打开适当的服务文件进行编辑。

# systemctl edit --full  varnish

找到以下行并更改-a开关的值,该开关指定监听地址和端口。将端口设置为80,如下截图所示。

请注意,如果您不指定地址,varnishd将侦听服务器上所有可用的IPv4IPv6接口。

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

保存文件中的更改并退出。

13.接下来,您需要定义Varnish将访问以获取内容的后端服务器。这是在Varnish主配置文件中完成的。

# vi /etc/varnish/default.vcl 

找到默认后端配置部分并将字符串“default”更改为server1(或您选择的任何名称以表示您的源服务器)。然后将端口设置为8080(或您在服务器块中定义的Nginx侦听端口)。

backend server1 {
    .host = "127.0.0.1";
    .port = "8080";
}
Set Default Varnish Backend Servers

对于本指南,我们在同一台服务器上运行VarnishNginx。如果您的Nginx Web服务器在不同的主机上运行。例如,另一台具有地址10.42.0.247的服务器,则设置.host参数如下。

backend server1 {
    .host = "10.42.0.247";
    .port = "8080";
}

保存文件并关闭它。

14.接下来,您需要重新加载systemd管理器配置,因为Varnish服务文件中的最近更改,然后重新启动Varnish服务以应用更改如下。

# systemctl daemon-reload
# systemctl restart varnish

15.现在确认NginxVarnish是否在配置的TCP套接字上监听。

# ss -tpln
Confirm Nginx and Varnish Ports

第四步:测试 Nginx Varnish 缓存设置

16. 接下来,验证网页是否通过 Varnish 缓存 进行服务。打开一个网络浏览器,使用服务器 IP 或 FDQN 进行导航,如下图所示。

http://www.tecmin.lan
OR
http://10.42.0.144
Verify Nginx Pages Served Via Varnish Cache

17. 或者,如下所示使用 curl 命令。如果是本地测试,请使用服务器的 IP 地址或网站的 FQDN,或者使用 127.0.0.1 或 localhost。

# curl -I http:///www.tecmint.lan
Verify Nginx Varnish Cache Page Using Curl

有用的 Varnish 缓存管理工具

18. 在这最后一节中,我们将简要介绍一些 Varnish 缓存 自带的有用实用程序,您可以使用这些程序来控制 varnishd,访问内存日志和总体统计信息等等。

varnishadm

varnishadm 是一个用于控制正在运行的 Varnish 实例的实用程序。它建立到 varnishd 的 CLI 连接。例如,您可以使用它来列出配置的后端,如下图所示(阅读 man varnishadm 获取更多信息)。

# varnishadm
varnish> backend.list
List Configured Backends

varnishlog

varnishlog 实用程序提供对请求特定数据的访问。它提供有关特定客户端和请求的信息(阅读 man varnishlog 获取更多信息)。

# varnishlog
Check Info About Clients and Requests

varnishstat

A varnishstat also known as varnish statistics, which gives you a glance at Varnish’s current performance by providing access to in-memory statistics such as cache hits and misses, information about the storage, threads created, deleted objects (read man varnishstat for more information).

# varnishstat 
Show Varnish Statistics

varnishtop

A varnishtop utility reads the shared memory logs and presents a continuously updated list of the most commonly occurring log entries (read man varnishtop for more information).

# varnishtop 
View Shared Memory logs

varnishhist

A varnishhist (varnish history) utility parses the varnish logs and outputs a continuously updated histogram showing the distribution of the last n requests by their processing (read man varnishhist for more information).

# varnishhist
Show Requests Histogram

這就是全部!在這個指南中,我們展示了如何在 Varnish Cache 上安裝並運行它,以加速在 CentOS/RHEL 8 中的網頁內容傳遞前面的 Nginx HTTP 伺服器。

對於這個指南的任何想法或問題,都可以使用下面的反饋表格進行分享。欲了解更多信息,請閱讀 Varnish Cache 文件

Varnish Cache 的主要缺點是它缺乏對 HTTPS 的原生支援。要在您的網站/應用程式上啟用 HTTPS,您需要配置一個 SSL/TLS 終結代理,與 Varnish Cache 一起工作,以保護您的網站。在我們的下一篇文章中,我們將展示如何在 CentOS/RHEL 8 上使用 Hitch 為 Varnish Cache 啟用 HTTPS。

Source:
https://www.tecmint.com/install-varnish-cache-for-nginx-on-centos-rhel-8/