WEB服务器-加速器Varnish

Varnish

Varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。
1 问题

通过配置Varnish缓存服务器,实现如下目标:
使用Varnish加速后端Web服务
代理服务器可以将远程的Web服务器页面缓存在本地
远程Web服务器对客户端用户是透明的
利用缓存机制提高网站的响应速度
使用varnishadm命令管理缓存页面
使用varnishstat命令查看Varnish状态
2 方案

通过源码编译安装Varnish缓存服务器
编译安装Varnish软件
修改配置文件,缓存代理源Web服务器,实现Web加速功能
使用3台RHEL7虚拟机,其中一台作为Web服务器(192.168.2.100)、一台作为Varnish代理服务器(192.168.4.5,192.168.2.5),另外一台作为测试用的Linux客户机(192.168.4.10),如图-2所示。
在这里插入图片描述
图-2
对于Web服务器的部署,此实验中仅需要安装nginx或者httpd软件、启动服务,并生成测试首页文件即可,默认httpd网站根路径为/var/www/html,首页文档名称为index.html,默认nginx网站根路径为/usr/local/nginx/html,默认首页为index.html。下面的实验我们以httpd为例作为Web服务器。
3 步骤

实现此案例需要按照如下步骤进行。
步骤一:构建Web服务器

1)使用yum安装web软件包

[root@web1 ~]# yum  -y  install  httpd

2)启用httpd服务(注意需要关闭nginx,否则端口冲突)

[root@web1 ~]# systemctl start httpd
httpd服务默认通过TCP 80端口监听客户端请求:
[root@web1 ~]# netstat  -anptu  |  grep httpd
tcp        0        0        :::80        :::*        LISTEN        2813/httpd

3)为Web访问建立测试文件
在网站根目录/var/www/html下创建一个名为index.html的首页文件:

[root@web1 ~]# cat /var/www/html/index.html 
192.168.2.100

4)测试页面是否正常(代理服务器测试后台web)

[root@proxy ~]# firefox http://192.168.2.100

步骤二:部署Varnish缓存服务器(192.168.4.5)
1)编译安装软件

[root@proxy ~]# yum -y install gcc readline-devel    //安装软件依赖包
[root@proxy ~]# yum -y install ncurses-devel         //安装软件依赖包
[root@proxy ~]# yum -y install pcre-devel            //安装软件依赖包
[root@proxy ~]# yum -y install python-docutils         //安装软件依赖包
 [root@proxy ~]# useradd -s /sbin/nologin varnish                //创建账户
[root@proxy ~]# tar -xf varnish-5.2.1.tar.gz
[root@proxy ~]# cd varnish-5.2.1
[root@proxy varnish-5.2.1]# ./configure
[root@proxy varnish-5.2.1]# make && make install

2)复制配置文件(注意相对路径与绝对路径)

[root@proxy varnish-5.2.1]# cp  etc/example.vcl   /usr/local/etc/default.vcl

3)修改代理配置文件

[root@proxy ~]# vim  /usr/local/etc/default.vcl
backend default {
    
    
     .host = "192.168.2.100";
     .port = "80";
 }

4)启动服务

[root@proxy ~]# varnishd  -f /usr/local/etc/default.vcl
//varnishd命令的其他选项说明如下:
//varnishd -s malloc,128M        定义varnish使用内存作为缓存,空间为128M
//varnishd -s file,/var/lib/varnish_storage.bin,1G 定义varnish使用文件作为缓存

步骤三:客户端测试

1)客户端开启浏览器访问

[root@client ~]# curl http://192.168.4.5

步骤四:其他操作

1)查看varnish日志

[root@proxy ~]# varnishlog                        //varnish日志
[root@proxy ~]# varnishncsa                    //访问日志

2)更新缓存数据,在后台web服务器更新页面内容后,用户访问代理服务器看到的还是之前的数据,说明缓存中的数据过期了需要更新(默认也会自动更新,但非实时更新)。

[root@proxy ~]# varnishadm  
varnish> ban req.url ~ .*
//清空缓存数据,支持正则表达式

猜你喜欢

转载自blog.csdn.net/weixin_45942735/article/details/104608278