varnish搭建反向代理

目录

 

需求:

配置文件:

步骤一:构建Web服务器

步骤二:部署Varnish缓存服务器(192.168.4.5)

步骤三

步骤四


需求:

使用Varnish加速后端Web服务,代理服务器可以将远程的Web服务器页面缓存在本地,远程Web服务器对客户端用户是透明的,利用缓存机制提高网站的响应速度,使用varnishadm命令管理缓存页面,使用varnishstat命令查看Varnish状态。

varnish搭建反向代理

配置文件:

步骤一:构建Web服务器

[[email protected] ~]# yum  -y  install  httpd

[[email protected] ~]# systemctl start httpd
[[email protected] ~]# setenforce 0
[[email protected] ~]# firewall-cmd --set-default-zone=trusted

[[email protected] ~]# netstat  -anptu  |  grep httpd
tcp        0        0        :::80        :::*        LISTEN        2813/httpd

[[email protected] ~]# cat /var/www/html/index.html 
192.168.2.100

[[email protected] ~]# firefox http://192.168.2.100

步骤二:部署Varnish缓存服务器(192.168.4.5)

[[email protected] ~]# yum -y install gcc readline-devel    //安装软件依赖包
[[email protected] ~]# yum -y install ncurses-devel         //安装软件依赖包
[[email protected] ~]# yum -y install pcre-devel            //安装软件依赖包
[[email protected] ~]# yum -y install \
python-docutils-0.11-0.2.20130715svn7687.el7.noarch.rpm         //安装软件依赖包
 [[email protected] ~]# useradd -s /sbin/nologin varnish                //创建账户
[[email protected] ~]# tar -xf varnish-5.2.1.tar.gz
[[email protected] ~]# cd varnish-5.2.1
[[email protected] varnish-5.2.1]# ./configure
[[email protected] varnish-5.2.1]# make && make install

[[email protected] varnish-5.2.1]# cp  etc/example.vcl   /usr/local/etc/default.vcl

[[email protected] ~]# vim  /usr/local/etc/default.vcl
backend default {
     .host = "192.168.2.100";
     .port = "80";
 }
[[email protected] ~]# varnishd  -f /usr/local/etc/default.vcl

//varnishd命令的其他选项说明如下:

//varnishd –s malloc,128M        定义varnish使用内存作为缓存,空间为128M

//varnishd –s file,/var/lib/varnish_storage.bin,1G 定义varnish使用文件作为缓存

 

步骤三

 

[[email protected] ~]# curl http://192.168.4.5

步骤四

1.查看varnish日志

[[email protected] ~]# varnishlog                        //varnish日志
[[email protected] ~]# varnishncsa                    //访问日志

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

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

1 ~]# firewall-cmd --set-default-zone=trusted

[[email protected] ~]# netstat  -anptu  |  grep httpd
tcp        0        0        :::80        :::*        LISTEN        2813/httpd

[[email protected] ~]# cat /var/www/html/index.html 
192.168.2.100

[[email protected] ~]# firefox http://192.168.2.100

步骤二:部署Varnish缓存服务器(192.168.4.5)

[[email protected] ~]# yum -y install gcc readline-devel    //安装软件依赖包
[[email protected] ~]# yum -y install ncurses-devel         //安装软件依赖包
[[email protected] ~]# yum -y install pcre-devel            //安装软件依赖包
[[email protected] ~]# yum -y install \
python-docutils-0.11-0.2.20130715svn7687.el7.noarch.rpm         //安装软件依赖包
 [[email protected] ~]# useradd -s /sbin/nologin varnish                //创建账户
[[email protected] ~]# tar -xf varnish-5.2.1.tar.gz
[[email protected] ~]# cd varnish-5.2.1
[[email protected] varnish-5.2.1]# ./configure
[[email protected] varnish-5.2.1]# make && make install

[[email protected] varnish-5.2.1]# cp  etc/example.vcl   /usr/local/etc/default.vcl

[[email protected] ~]# vim  /usr/local/etc/default.vcl
backend default {
     .host = "192.168.2.100";
     .port = "80";
 }
[[email protected] ~]# varnishd  -f /usr/local/etc/default.vcl

//varnishd命令的其他选项说明如下:

//varnishd –s malloc,128M        定义varnish使用内存作为缓存,空间为128M

//varnishd –s file,/var/lib/varnish_storage.bin,1G 定义varnish使用文件作为缓存

步骤三

[[email protected] ~]# curl http://192.168.4.5

步骤四

1.查看varnish日志

[[email protected] ~]# varnishlog                        //varnish日志
[[email protected] ~]# varnishncsa                    //访问日志

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

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