在CentOS 7上安装Apache/nginx的Varnish缓存6
varnish缓存是Web应用程序加速器,通常称为缓存HTTP反向代理。
消失通常安装在Web服务器前面,例如Apache或者Nginx,并配置为缓存这些服务器的内容。
清漆缓存真的,真的很快。
它通常会加速递送300-1000x,具体取决于架构。
本教程将通过在CentOS 7服务器上安装清漆缓存6.
这可以是虚拟机,LXC容器或者专用服务器。
在CentOS 7上安装清漆缓存6时要遵循的步骤是简单的。
第1步:添加epel存储库
要安装的一些依赖项需要Epel存储库设置。
只需运行以下命令将EPEL添加到CentOS 7系统。
sudo yum install -y epel-release
"-y"表示下载并安装了包之前没有提示。
第2步:安装依赖性包
如果通过Repo Management Tool添加存储库,则需要安装Pygpgme&Yum-Utils。
sudo yum -y install pygpgme yum-utils
第3步:添加varnish缓存存储库
在此步骤中,我们为CentOS添加了rannish缓存存储库7.复制下面的命令并将其粘贴在终端上。
使用sudo访问的用户帐户。
sudo tee /etc/yum.repos.d/varnish60lts.repo<<EOF [varnish60lts] name=varnishcache_varnish60lts baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/x86_64 repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish60lts/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300 EOF
第4步:在CentOS 7上安装Varnish 6
在CentOS 7系统上,更新yum cache是可选的。
让我们只需在安装应用程序之前更新本地yum缓存。
其中我们仅限于添加的存储库的更新。
$sudo yum -q makecache -y --disablerepo='*' --enablerepo='varnish60lts' Importing GPG key 0xA750EDCD: Userid : "https://packagecloud.io/varnishcache/varnish60lts (https://packagecloud.io/docs#gpg_signing) Hyman@theitroad" Fingerprint: 48d8 1a24 cb04 56f5 d594 31d9 4cfc fd6b a750 edcd From : https://packagecloud.io/varnishcache/varnish60lts/gpgkey
然后在CentOS 7系统上安装Varnish 6.
$sudo yum install varnish Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirror.23media.com * epel: mirror.23media.com * extras: mirror.alpha-labs.net * remi-php73: mirror.23media.com * remi-safe: mirror.23media.com * updates: mirror.alpha-labs.net Resolving Dependencies --> Running transaction check ---> Package varnish.x86_64 0:6.0.3-1.el7 will be obsoleting ---> Package varnish-libs.x86_64 0:4.1.11-1.el7 will be obsoleted --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================== Installing: varnish x86_64 6.0.3-1.el7 varnish60lts 1.8 M replacing varnish-libs.x86_64 4.1.11-1.el7 Transaction Summary =================================================================================================================================================== Install 1 Package Total download size: 1.8 M Is this ok [y/d/N]: y Downloading packages: varnish-6.0.3-1.el7.x86_64.rpm | 1.8 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : varnish-6.0.3-1.el7.x86_64 1/2 Erasing : varnish-libs-4.1.11-1.el7.x86_64 2/2 Verifying : varnish-6.0.3-1.el7.x86_64 1/2 Verifying : varnish-libs-4.1.11-1.el7.x86_64 2/2 Installed: varnish.x86_64 0:6.0.3-1.el7 Replaced: varnish-libs.x86_64 0:4.1.11-1.el7 Complete!
有关已安装包的更多信息,可以使用:
$rpm -qi varnish
关于CentOS上的清漆安装的关键说明。
主rsnish配置文件是/etc/varnish/default.vclvarnish秘密文件:/etc/ramnish/secretvarnish缓存可执行文件二进制文件:/usr/sbin/varnishdvarnish systemd单元文件:/lib/systemd /system/varnish.service和/etc/systemd/system/multi-user.target.wants/varnish.service.
第5步:在CentOS 7上启动和启用纸值
启动并启用"从系统启动启动"清漆进程"。
sudo systemctl enable --now varnish
验证使用以下命令是否正在运行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) since Mon 2019-05-27 20:34:51 EAT; 7s ago Process: 7380 ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS) Main PID: 7381 (varnishd) CGroup: /system.slice/varnish.service ├─7381 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m └─7391 /usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m Jan 27 20:34:50 c4geeks-app-server systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator… Jan 27 20:34:51 c4geeks-app-server varnishd[7380]: Debug: Version: varnish-6.0.3 revision 7d1ded3aa033a018317dbafc61587026ea2ef8a3 Jan 27 20:34:51 c4geeks-app-server varnishd[7381]: Version: varnish-6.0.3 revision 7d1ded3aa033a018317dbafc61587026ea2ef8a3 Jan 27 20:34:51 c4geeks-app-server varnishd[7380]: Debug: Platform: Linux,3.10.0-957.1.3.el7.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Jan 27 20:34:51 c4geeks-app-server varnishd[7381]: Platform: Linux,3.10.0-957.1.3.el7.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit Jan 27 20:34:51 c4geeks-app-server varnishd[7381]: Child (7391) Started Jan 27 20:34:51 c4geeks-app-server varnishd[7380]: Debug: Child (7391) Started Jan 27 20:34:51 c4geeks-app-server varnishd[7381]: Child (7391) said Child starts Jan 27 20:34:51 c4geeks-app-server systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.
从输出中,我们可以看到状态为"正在运行",这意味着我们的清漆安装已准备好使用。
步骤6:为nginx/apache web服务器配置varnish缓存
我们早些时候讨论了清漆缓存是一个缓存的HTTP反转代理,它位于Web服务器前面。
目标是加快Web服务器。
安装我们喜欢的Web服务器 - 此演示显示nginx/apache httpd服务器的安装。
# Apache $sudo yum -y install httpd # Nginx $sudo yum -y install nginx
1.配置nginx与rannish缓存一起使用
默认情况下,nginx在TCP端口80上侦听,我们需要将侦听端口更改为8080. rarnish缓存将使用端口80。
$sudo vi /etc/nginx/nginx.conf ..... server { listen 8080 default_server; .... }
如果使用虚拟托管功能,请编辑相关的配置文件,例如,
$sudo vi /etc/nginx/conf.d/mysite.conf
然后重新启动nginx。
$sudo systemctl restart nginx
确认设置。
$ss -tunelp | grep 8080 tcp LISTEN 0 128 *:8080 *:* users:(("nginx",pid=13073,fd=6),("nginx",pid=13072,fd=6),("nginx",pid=13071,fd=6)) ino:34626 sk:ffff8af4b7a9ec80 <-> tcp LISTEN 0 128 :::8080 :::* users:(("nginx",pid=13073,fd=7),("nginx",pid=13072,fd=7),("nginx",pid=13071,fd=7)) ino:34627 sk:ffff8af52e828840 v6only:1 <->
2.配置Apache以使用rannish缓存
如果我们正在使用Apache Web服务器,请将侦听端口设置为8080。
$sudo vi /etc/httpd/conf/httpd.conf ... Listen 8080
也可以使用单个SED命令。
sudo sed -i "s/Listen 80/Listen 8080/" /etc/httpd/conf/httpd.conf
更改后重新启动httpd服务。
sudo systemctl restart httpd
3.配置Varnish Cache Server
现在编辑varnish缓存配置文件并将侦听端口设置为80。
sudo vi /etc/systemd/system/multi-user.target.wants/varnish.service
编辑以execstart开头的行,然后从以下开始更改:
ExecStart=/usr/sbin/varnishd -a :6081 -f /etc/varnish/default.vcl -s malloc,256m
到:
ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
然后重新启动varnish systemd服务。
sudo systemctl daemon-reload sudo systemctl restart varnish
确认varnish使用的端口。
$sudo 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) since Tue 2019-05-28 10:27:07 CEST; 13s ago Process: 13368 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS) Main PID: 13369 (varnishd) CGroup: /system.slice/varnish.service ├─13369 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m └─13379 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
确保将nginx/apache配置为rannish代理的后端服务器。
$sudo vi /etc/varnish/default.vcl ..... # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } .......
注意端口8080在Apache/nginx Web服务器上配置。
对于多个后端,配置将看出:
backend default { .host = "127.0.0.1"; .port = "8080"; } backend java { .host = "127.0.0.1"; .port = "8000"; }
但是,我们需要讲述amish使用vcl_recv发送差异URL的地方。
让我们说我们的Java应用程序应该处理以/java/over开头的URL。
sub vcl_recv { if (req.url ~ "^/java/") { set req.backend_hint = java; } else { set req.backend_hint = default; } }
查看更多使用多个后端。
第7步:在nginx/apache上测试varnish缓存
最后,我们测试是否启用了rannish缓存并使用下面的Curl命令使用Apache/nginx服务:
$curl -I http://localhost HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 28 Jan 2019 08:51:32 GMT Content-Type: text/html Content-Length: 3700 Last-Modified: Fri, 10 Jan 2019 08:08:40 GMT ETag: "5cd53188-e74" X-Varnish: 65544 Age: 0 Via: 1.1 varnish (Varnish/6.0) Accept-Ranges: bytes Connection: keep-alive
这为我们提供了HTTP标题信息。
如果我们重新运行该命令,它将显示旧式缓存的响应(请注意 Age
标题):
$curl -I http://localhost HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 28 Jan 2019 08:51:32 GMT Content-Type: text/html Content-Length: 3700 Last-Modified: Fri, 10 Jan 2019 08:08:40 GMT ETag: "5cd53188-e74" X-Varnish: 65547 65545 Age: 44 Via: 1.1 varnish (Varnish/6.0) Accept-Ranges: bytes Connection: keep-alive
它应该与DNS记录集的有效域名相同。
$curl -I https://blog.theitroad.com HTTP/2 200 date: Tue, 28 Jan 2019 09:09:20 GMT content-type: text/html set-cookie: __cfduid=de1acba723fc04cdfb58c84af80ac1b401559034559; expires=Wed, 27-May-20 09:09:19 GMT; path=/; domain=.theitroad.com; HttpOnly; Secure last-modified: Fri, 10 Jan 2019 08:08:40 GMT x-varnish: 65557 17 age: 22 via: 1.1 varnish (Varnish/6.0) accept-ranges: bytes expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" server: cloudflare cf-ray: 4ddf16ce9d86cb77-MBA