Varnish

概述

Varnish Cache, a high-performance HTTP accelerator

处理缓存的顺序:接受到请求 –- 分析请求(分析你的URL,分析你的首部) – hash计算 – 查找缓存 – 新鲜度检测 — 访问源 — 缓存 – 建立响应报文 – 响应并记录日志。

Install Varnish 6.0LTS on CentOS7

yum install pygpgme yum-utils -y

cat << 'EOF' > /etc/yum.repos.d/varnishcache_varnish60lts.repo
[varnishcache_varnish60lts]
name=varnishcache_varnish60lts
baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/7/$basearch
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

[varnishcache_varnish60lts-source]
name=varnishcache_varnish60lts-source
baseurl=https://packagecloud.io/varnishcache/varnish60lts/el/6/SRPMS
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

yum clean all && yum list

yum list varnish --showduplicates | sort -r
yum install varnish

==================================================================================================================
 Package                         Arch       Version                      Repository                          Size
==================================================================================================================
Installing:
 varnish                         x86_64     6.0.6-1.el7                  varnishcache_varnish60lts          1.8 M
Installing for dependencies:
 cpp                             x86_64     4.8.5-39.el7                 base                               5.9 M
 gcc                             x86_64     4.8.5-39.el7                 base                                16 M
 glibc-devel                     x86_64     2.17-307.el7.1               base                               1.1 M
 glibc-headers                   x86_64     2.17-307.el7.1               base                               689 k
 jemalloc                        x86_64     3.6.0-1.el7                  epel                               105 k
 kernel-headers                  x86_64     3.10.0-1127.13.1.el7         updates                            9.0 M
 libmpc                          x86_64     1.0.1-3.el7                  base                                51 k
 mpfr                            x86_64     3.1.1-4.el7                  base                               203 k
==================================================================================================================

[root@astest ~]# rpm -ql varnish | fgrep -v '/usr/share'
/etc/ld.so.conf.d/varnish-x86_64.conf
/etc/logrotate.d/varnish
/etc/varnish
/etc/varnish/default.vcl
/usr/bin/varnishadm
/usr/bin/varnishhist
/usr/bin/varnishlog
/usr/bin/varnishncsa
/usr/bin/varnishstat
/usr/bin/varnishtest
/usr/bin/varnishtop
/usr/lib/systemd/system/varnish.service
/usr/lib/systemd/system/varnishncsa.service
/usr/lib64/libvarnishapi.so.1
/usr/lib64/libvarnishapi.so.1.0.6
/usr/lib64/varnish
/usr/lib64/varnish/vmods
/usr/lib64/varnish/vmods/libvmod_blob.so
/usr/lib64/varnish/vmods/libvmod_directors.so
/usr/lib64/varnish/vmods/libvmod_proxy.so
/usr/lib64/varnish/vmods/libvmod_purge.so
/usr/lib64/varnish/vmods/libvmod_std.so
/usr/lib64/varnish/vmods/libvmod_unix.so
/usr/lib64/varnish/vmods/libvmod_vtc.so
/usr/sbin/varnishd
/usr/sbin/varnishreload
/var/lib/varnish
/var/log/varnish

cp -a  /etc/varnish/default.vcl{,.default}

安装nginx模拟后端测试

cat << 'EOF' > /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

yum install nginx-1.16.1 -y

systemctl start nginx
curl 127.0.0.1:80
cat /usr/share/nginx/html/index.html

配置加速后端的80服务

[root@astest ~]# cat   /etc/varnish/default.vcl
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "80";
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT cache";
    }
    else {
        set resp.http.X-Cache = "Miss cache";
    }
}


systemctl start varnish.service
curl -I 127.0.0.1:6081

在这里插入图片描述 如上图所示,访问 varnish:6081 –> nginx:80,”Miss cache”表示未命中缓存,”HIT cache”表示击中缓存

Varnish 启停

ls /var/log/varnish
cat /usr/lib/systemd/system/varnish.service (可以看到具体的启动明令及监听的端口)

systemctl start varnish.service
systemctl enable varnish.service
systemctl status varnish.service

[root@astest ~]# netstat -lntup | grep var
tcp        0      0 0.0.0.0:6081            0.0.0.0:*               LISTEN      11601/varnishd      
tcp        0      0 127.0.0.1:34058         0.0.0.0:*               LISTEN      11601/varnishd      
tcp6       0      0 :::6081                 :::*                    LISTEN      11601/varnishd      

配置缓存多个站点参考

假设有如下两个域名及对应的服务
192.168.1.7:80  test1.sina.cn
192.168.1.8:80  test2.sina.cn

参考varnish配置文件如下
[root@astest ~]# cat   /etc/varnish/default.vcl
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend test1 { 
    .host = "192.168.1.7";
    .port = "80";
}
backend test2 {
    .host = "192.168.1.8";
    .port = "80";
}

sub vcl_recv {
    if (req.http.host ~ "^(www.)?you.cn"){
        set req.http.host = "test1.sina.cn";
        set req.backend_hint = test1;
    } elsif (req.http.host ~ "^test2.sina.cn") {
        set req.backend_hint = test2;
    }
}

改配置文件后需要重启服务 systemctl restart varnish

参考资料

https://www.cnblogs.com/diantong/p/11300705.html
https://varnish-cache.org/docs/6.0/

csdn 109822727