Varnish的简单配置及使用

一.Varnish的简单介绍
1.varnish是什么
Varnish是一个web加速器,被安装在web应用程序前面,缓存web应用程序,并响应用户请求。
是一款具有高性能的开源HTTP加速器,具有方向代理及缓存的功能,功能与squid服务器相似都可以做HTTP缓存。
与squid的区别
squid是从硬盘读取缓存数据,而varnish是把数据存放在内存中,直接读取内存,避免了频繁在内存、磁盘中交换文件,所以Varnish要相对更高效,但也有缺点,内存中的缓存在服务器重启后会丢失
2.Varnish的进程
Varnish开启产生的进程主要有两个,一个由root开启,一个由varnish开启

[root@server1 ~]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     13715  0.0  4.6 122784 87908 ?        SLs  Jun15   0:00 /usr/sbin/varni  ##root开启
varnish  13716  0.0  4.9 271768 93188 ?        Sl   Jun15   0:01 /usr/sbin/varni  ##varnish开启

查看root开启的varnish进程

[root@server1 ~]# cd /proc
[root@server1 proc]# cd 13715
[root@server1 13715]# cat status

在这里插入图片描述
查看varnish开启的进程

[root@server1 proc]# cd 13716
[root@server1 13716]# cat status

在这里插入图片描述
由此可知:varnish工作时,由root用户开启varnish,由varnish开启下面的子线程来进行请求处理、管理varnish等
Varnish主要有两个进程:manage进程及child进程
(1)manage进程
用于更新配置,vcl文件的编译,varnish的监控,初始化varnish及提供varnish管理接口
(2)child进程
主要用于进行请求任务的处理,接受请示等
child进程中各线程的任务处理工作
1)accept线程:监听端口,接受连接;接受连接后会组成session结构,查看是否有空闲线程,若有则分配给其处理,若无,则检查等待队列overflow的大小。若过大则抛弃请求;否则加入overflow队列
2)work线程:从overflow队列中获取任务,由varnish进行任务处理,处理完成后由pipe线程实现通信,传递给epoll线程,等待下一个事件的发送
3)Worker threads作用:处理;child子进程会给每一个用户的请求进行处理,处理一个则开启一个worker进程,所以它是单线程,单响应的;即每一个用户的请求是由一个独立的线程响应的
4)epoll线程:将事件发送时对应的session,放入overflow队列,以供work线程从中取出求求继续处理。在等待事件发送时同时会去检查session是否过期
5)expire线程:对以二叉树形式组织的缓存对象进行过期检查,对过期的对象进行处理会更新或者弃用
在这里插入图片描述
3.Varnish如何工作
(1)初始化过程
Varnish 的master进程负责启动工作,master进程读取配置文件,根据指定的空间大小来创建存储空间,创建并管理child进程
child进程来处理后续任务,它会分配一些线程来执行不同的工作。例如:接受http的请求;为缓存对象分配存储空间;清除过期缓存对象;释放空间,碎片整理等
(2)http请求处理过程
由专门负责接收http请求的线程一直监控http请求端口,当有请求时会唤起一个工作线程。这个工作线程会分析http的请求,并清楚了解需求的内容开始在缓存中查找是否有这个请求对象内容。如果请求内容存在,会把缓存对象直接返回给用户;如果请求不存在,他会把请求转给后端服务器处理,并等待结果,工作线程从后端得到请求的答案后会先把请求答案缓存进一个内存空间用以备份,下次访问时时会快速响应从而节约时间,然后把请求答案返回给用户。
(3)分配缓存过程
有一个对象需要缓存时,根据这个对象的大小,到空闲缓存区中查找大小最适合的空闲块,找到后就把这个对象放进去
如果这个对象没有填满这个空闲块,就把剩余的空间做为一个新的空闲块
如果空闲缓存区中没地方了,就要先删除一部分缓存来腾出地方,删除是根据最近最少使用原则
(4)释放缓存过程
有一个线程来负责缓存的释放工作,他定期检查缓存中所有对象的生存周期,如果某个对象在指定的时间段内没有被访问,就把这个对象删除,释放其占用的缓存空间
释放空间后,检查一下临近的内存空间是否是空闲的,如果是,就整合为一个更大的空闲块,实现空间碎片的整理
二.Varnish的安装部署
1.环境:三台主机(其中一台作为Varnish部署主机)
server1 172.25.4.111 Varnish部署主机
server2 172.25.4.112
server3 172.25.4.113
2.下载安装包

[root@server1 mnt]# cd varnish/
[root@server1 varnish]# ls
12.25                            varnish-4.0.5-1.el7.x86_64.rpm
bansys.zip                       varnish-libs-3.0.4-1.el6.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm  varnish-libs-3.0.5-1.el6.x86_64.rpm
rhel6 varnish.pdf                varnish-libs-4.0.5-1.el7.x86_64.rpm
varnish-3.0.4-1.el6.x86_64.rpm   Varnish权威指南-中文版.pdf
varnish-3.0.5-1.el6.x86_64.rpm   初始阶段的网站架构.dps
[root@server1 varnish]# yum install -y varnish-4.0.5-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm jemalloc-3.6.0-1.el7.x86_64.rpm
[root@server1 varnish]# rpm -qc varnish-4.0.5-1.el7.x86_64  ##下载成功
/etc/logrotate.d/varnish
/etc/varnish/default.vcl
/etc/varnish/varnish.params

3.Varnish配置
(1)查看配置文件
[root@server1 varnish]# vim /usr/lib/systemd/system/varnish.service
在这里插入图片描述
由此可知:
Varnish打开的最大文件数限制为131072
Varnish锁定的共享内存大小为8200k即8G
(2)临时更改共享内存及最大文件数

[root@server1 varnish]# ulimit -a  ##查看
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3885
max locked memory       (kbytes, -l) 64  ##共享内存最大为6400k
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024  ##打开的文件数最大为1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3885
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@server1 varnish]# ulimit -n 131072  ##更改最大文件数
[root@server1 varnish]# ulimit -l 82  ##更改最大共享内存
[root@server1 varnish]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 3885
max locked memory       (kbytes, -l) 82  ##最大共享内存更改成功
max memory size         (kbytes, -m) unlimited
open files                      (-n) 131072  ##最大文件数更改成功
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 3885
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

(3)永久配置Varnish

[root@server1 ~]# vim /etc/security/limits.conf   ##永久更改最大文件数及最大共享内存

在这里插入图片描述

[root@server1 ~]# vim /etc/varnish/default.vcl  ##添加代理服务器ip及端口

在这里插入图片描述

[root@server1 ~]# vim /etc/varnish/varnish.params  ##更改端口

在这里插入图片描述

[root@server1 ~]# systemctl restart varnish

(4)测试
以server2作为需要被代理的服务端

[root@server2 ~]# yum install httpd -y
[root@server2 ~]# systemctl start httpd
[root@server2 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@server2 ~]# cd /var/www/html
[root@server2 html]# ls
[root@server2 html]# vim index.html
[root@server2 html]# systemctl restart httpd
[root@server2 html]# cat index.html
server2

以server3作为客户端需要访问服务端,则可以通过varnish代理端去访问

[root@server3 ~]# curl 172.25.4.111
server2  ##访问成功

三.Varnish缓存命中情况
server1作为varnish代理端,在配置文件中添加了后端服务器的ip及端口,在访问代理端的时候实际是在访问后端服务器,则第一次访问会产生缓存,下一次访问的时侯则由缓存服务器直接响应客户端需求,减少延时
修改配置文件,在缓存数据时发送信息给客户端(查看缓存命中情况)

[root@server1 ~]# vim /etc/varnish/default.vcl 
[root@server1 ~]# systemctl restart varnish

在这里插入图片描述
在服务端进行测试缓存命中

[root@server3 ~]# curl -I 172.25.4.111
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 13:48:37 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache  ##未命中
Connection: keep-alive

[root@server3 ~]# curl -I 172.25.4.111
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 13:48:37 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 5 3
Age: 2
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache  ##命中并开始产生缓存
Connection: keep-alive

[root@server3 ~]# curl -I 172.25.4.111
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 13:48:37 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770 3
Age: 5
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache
Connection: keep-alive

在代理端清除所有缓存

[root@server1 ~]# varnishadm ban req.url "~" /  ##清除所有缓存

客户端测试:

[root@server3 ~]# curl -I 172.25.4.111
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 13:49:29 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32772
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache  ##未命中
Connection: keep-alive

[root@server3 ~]# curl -I 172.25.4.111
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 13:49:29 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 9 32773
Age: 2
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache  ##命中
Connection: keep-alive

四.定义多个不同域名站点的后端服务器
将server2、server3作为后端服务器;真机作为客户端进行实验
定义2个后端服务器并且当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,访问其他页面报错

[root@server1 ~]# vim /etc/varnish/default.vcl 
[root@server1 ~]# systemctl restart varnish

在这里插入图片描述
在真机客户端添加解析并进行测试

[root@foundation4 ~]$ vim /etc/hosts

在这里插入图片描述

[kiosk@foundation4 ~]$ curl westos.org
server2
[kiosk@foundation4 ~]$ curl bbs.westos.org
server3
[kiosk@foundation4 ~]$ curl www.westos.org
server2

五.轮询–负载均衡
目的:减轻后端服务器被访问的压力(对同一域名进行后端服务端的轮询)
当访问www.westos.org时让server2与server3轮询做后台服务端

[root@server1 ~]# vim /etc/varnish/default.vcl 
[root@server1 ~]# systemctl restart varnish

在这里插入图片描述
客户端测试

[kiosk@foundation4 ~]$ curl bbs.westos.org
server3
[kiosk@foundation4 ~]$ curl www.westos.org
server2
[kiosk@foundation4 ~]$ curl www.westos.org
server3
[kiosk@foundation4 ~]$ curl www.westos.org
server2
[kiosk@foundation4 ~]$ curl www.westos.org
server3

当采用轮询时,两个域名都会访问server3,无法识别,所以可以通过创建虚拟主机的方法来让不同的域名访问不同的内容
在后端服务器3中创建虚拟主机

[root@server3 html]# vim /etc/httpd/conf.d/vhost.conf
[root@server3 html]# systemctl restart httpd

在这里插入图片描述

[root@server3 html]# mkdir /www
[root@server3 html]# mkdir /bbs
[root@server3 html]# vim /www/index.html
[root@server3 html]# cat /www/index.html
server3:www.westos.org
[root@server3 html]# vim /bbs/index.html
[root@server3 html]# cat /bbs/index.html
server3:bbs.westos.org

主机客户端进行测试

[kiosk@foundation4 ~]$ curl www.westos.org
server2
[kiosk@foundation4 ~]$ curl www.westos.org
server3:www.westos.org
[kiosk@foundation4 ~]$ curl bbs.westos.org
server3:bbs.westos.org
[kiosk@foundation4 ~]$ curl www.westos.org
server2
[kiosk@foundation4 ~]$ curl www.westos.org
server3:www.westos.org
[kiosk@foundation4 ~]$ curl bbs.westos.org
server3:bbs.westos.org

六.推送平台的创建
1.下载php软件

[root@server1 varnish]# yum install php -y

2.解压平台安装包到公共目录里

[root@server1 ~]# cd /mnt/varnish/
[root@server1 varnish]# ls
12.25                            varnish-4.0.5-1.el7.x86_64.rpm
bansys.zip                       varnish-libs-3.0.4-1.el6.x86_64.rpm
jemalloc-3.6.0-1.el7.x86_64.rpm  varnish-libs-3.0.5-1.el6.x86_64.rpm
rhel6 varnish.pdf                varnish-libs-4.0.5-1.el7.x86_64.rpm
varnish-3.0.4-1.el6.x86_64.rpm   Varnish权威指南-中文版.pdf
varnish-3.0.5-1.el6.x86_64.rpm   初始阶段的网站架构.dps
[root@server1 varnish]# unzip bansys.zip -d /var/www/html
[root@server1 varnish]# cd /var/www/html
[root@server1 html]# ls
bansys
[root@server1 html]# mv bansys/* /var/www/html
[root@server1 html]# ls
bansys  class_socket.php  config.php  index.php  purge_action.php  static

3.更改php配置文件

[root@server1 html]# vim config.php

在这里插入图片描述
4.更改http端口为8080

[root@server1 conf.d]# cd /etc/httpd/conf
[root@server1 conf]# ls
httpd.conf  magic
[root@server1 conf]# vim httpd.conf 
[root@server1 conf]# systemctl restart httpd

更改原因:http与varnish的默认端口均为80,当以相同断空进行访问时会因为端口冲突而失败
在这里插入图片描述
5.客户端进行测试:http://172.25.4.111:8080/
在这里插入图片描述

[kiosk@foundation4 ~]$ curl -I www.westos.org
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 15:53:26 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache  ##未命中
Connection: keep-alive

[kiosk@foundation4 ~]$ curl -I www.westos.org
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 15:53:26 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 5 3
Age: 3
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache  ##命中并开始缓存
Connection: keep-alive

[kiosk@foundation4 ~]$ curl -I www.westos.org
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 15:53:26 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770 3
Age: 4
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache
Connection: keep-alive

客户端进行缓存清理
bansys有两种工作模式,分别未telnet和http模式,实验采用http模式
在这里插入图片描述

[kiosk@foundation4 ~]$ curl -I www.westos.org
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 15:55:38 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 32772
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache  ##未命中
Connection: keep-alive

[kiosk@foundation4 ~]$ curl -I www.westos.org
HTTP/1.1 200 OK
Date: Sat, 15 Jun 2019 15:55:38 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Sat, 15 Jun 2019 13:10:57 GMT
ETag: "8-58b5c7e6add10"
Content-Length: 8
Content-Type: text/html; charset=UTF-8
X-Varnish: 9 32773
Age: 2
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache  ##命中并产生缓存
Connection: keep-alive
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值