Varnish Cache是一个高性能的HTTP加速器,专为高流量的动态网站设计。它在你的客户和服务器之间充当中间人,在所有入站请求落到你的网络服务器的后端之前处理它们。Varnish对网络服务器提供的每个网页都进行了缓存。当用户请求一个缓存的页面时,Varnish提供缓存的副本,而不是从后端服务器反复请求同一个页面。这使得Varnish的全页面缓存对于拥有高流量商店和成千上万产品的商店来说是如此理想。
这篇文章将告诉你如何在Ubuntu 22.04上安装Varnish Cache并将其与Apache一起配置。
apt install apache2 -y
接下来,你需要编辑Apache配置文件,将默认端口改为不同的端口。
nano /etc/apache2/ports.conf
找到下面这一行。
Listen 80
然后,将其替换为以下一行。
Listen 8080
保存并关闭该文件,然后编辑Apache默认的虚拟主机配置文件。
nano /etc/apache2/sites-available/000-default.conf
找到下面这一行。
<VirtualHost *:80>
并将其替换为以下一行。
<VirtualHost *:8080>
systemctl restart apache2
ss -antpl | grep 8080
LISTEN 0 511 *:8080 *:* users:(("apache2",pid=2553,fd=4),("apache2",pid=2552,fd=4),("apache2",pid=2551,fd=4))
apt install debian-archive-keyring curl gnupg apt-transport-https -y
curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg
nano /etc/apt/sources.list.d/varnishcache_varnish70.list
deb https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
nano /etc/apt/preferences.d/varnish
Package: varnish Pin: origin packagecloud.io Pin-Priority: 900
apt update
apt install varnish -y
nano /etc/varnish/default.vcl
backend default { .host = "127.0.0.1"; .port = "8080"; }
mkdir /etc/systemd/system/varnish.service.d
nano /etc/systemd/system/varnish.service.d/customport.conf
[Service] ExecStart= ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m
systemctl daemon-reload
systemctl restart varnish
systemctl status varnish
? varnish.service - Varnish Cache, a high-performance HTTP accelerator Loaded: loaded (/lib/systemd/system/varnish.service; enabled; vendor preset: enabled) Drop-In: /etc/systemd/system/varnish.service.d ??customport.conf Active: active (running) since Tue 2022-10-18 13:07:44 UTC; 14s ago Process: 4968 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m (c> Main PID: 4969 (varnishd) Tasks: 217 Memory: 90.6M CPU: 595ms CGroup: /system.slice/varnish.service ??4969 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m ??4983 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,256m Oct 18 13:07:43 ubuntu2204 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator... Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Version: varnish-7.0.3 revision 6a4c6a5c7e66a664b140278c209f0b18c544cab8 Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Platform: Linux,5.15.0-33-generic,x86_64,-junix,-smalloc,-sdefault,-hcritbit Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Child (4983) Started Oct 18 13:07:44 ubuntu2204 varnishd[4969]: Child (4983) said Child starts Oct 18 13:07:44 ubuntu2204 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
在这一点上,Varnish已经安装完毕,并且在80端口上进行监听。你可以用下面的命令检查它。
ss -antpl | grep :80
你将得到以下输出:
LISTEN 0 1024 0.0.0.0:80 0.0.0.0:* users:(("cache-main",pid=4983,fd=3),("varnishd",pid=4969,fd=3)) LISTEN 0 1024 [::]:80 [::]:* users:(("cache-main",pid=4983,fd=5),("varnishd",pid=4969,fd=5)) LISTEN 0 511 *:8080 *:* users:(("apache2",pid=4749,fd=4),("apache2",pid=4748,fd=4),("apache2",pid=4745,fd=4))
curl -I http://localhost/
你会在下面的输出中得到Varnish的缓存。
HTTP/1.1 200 OK Date: Tue, 18 Oct 2022 13:08:27 GMT Server: Apache/2.4.52 (Ubuntu) Last-Modified: Tue, 18 Oct 2022 13:03:09 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/7.0) ETag: W/"29af-5eb4eb6b9e071-gzip" Accept-Ranges: bytes Content-Length: 10671 Connection: keep-alive
上一条: 在CentOS 7上安装MariaDB
下一条: 斯里兰卡.lk域名注册机构介绍