Varnish是一款高性能且开源的反向代理服务器和http加速器。
与传统的Squid相比,Varnish具有性能更高、速度更快、管理更方便等诸多优点。
下面由varnish源码包搭建一个web反向代理服务器。
在web端构建web服务。
web服务器基本环境。
[root@server ~]# ifconfig | grep -w inet | awk ‘{print $2}’
192.168.139.10
web服务器本人使用yum安装nginx。
[root@server ~]# yum list | grep ^nginx && yum -y install nginx*
创建一个HTML访问url;
[root@server ~]# mkdir /usr/share/nginx/html/hello
[root@server ~]# echo “hello world” > /usr/share/nginx/hello/index.html
启动nginx
[root@server ~]# systemctl start nginx
部署Varnish缓存服务器。
下载varnish源码包。
访问地址:http://varnish-cache.org/releases/
本人在这里选择的是比较老的一个版本,3.0.7的源码包。
服务器基本环境说明。
[root@linux ~]# nmcli connection show
NAME UUID TYPE DEVICE
etc1 2b038af1-c1ec-49e1-bcec-20a32855ee62 ethernet ens33
有线连接 1 c652c162-bc9d-324a-aefc-f56f0d1f3558 ethernet ens37
[root@linux ~]# ifconfig | grep -w inet | awk ‘{print $2}’
192.168.139.175
192.168.101.9
安装依赖软件。
[root@linux ~]# yum -y install gcc readline-devel pcre-devel
创建varnish用户和组。
[root@linux ~]# groupadd varnish
[root@linux ~]# useradd -g varnish varnish -M -s /sbin/nologin
解压源码包,进入安装目录文件夹。
[root@linux ~]# tar -xf varnish-3.0.7.tgz
[root@linux ~]# cd varnish-3.0.7/
开始编译安装软件。
[root@linux ~]# ./configure --prefix=/usr/local/varnish
在安装之前来一个小小的普及,make编译时可以指定有多少个CPU同时进行编译,默认不指定是一个CPU进行编译。
查看本机CPU核心数。
[root@linux ~]# cat /proc/cpuinfo| grep “processor”| wc -l
6
[root@linux ~]# make -j 6 && make install
复制启动脚本及配置文件.
[root@linux ~]# cp redhat/varnish.initrc /etc/init.d/varnish
[root@linux ~]# cp redhat/varnish.sysconfig /etc/sysconfig/varnish
配置环境变量。
[root@linux ~]# vim /etc/profile
...
export VARNISH_HOME=/usr/local/varnish
export PATH=$VARNISH_HOME/bin:$VARNISH_HOME/sbin:$PATH
[root@linux ~]# source /etc/profile
建立命令软连接。
[root@linux ~]# ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
[root@linux ~]# ln -s /usr/local/varnish/bin/* /usr/sbin/
修改Varnish文件。
[root@linux ~]# vim /etc/sysconfig/varnish
66行:VARNISH_LISTEN_PORT=80 #默认端口
89行:VARNISH_STORAGE_SIZE=4G #定义缓存大小
92行:VARNISH_STORAGE="malloc,${ VARNISH_STORAGE_SIZE} " #基于内存方式缓存
修改代理配置文件。
[root@linux ~]# mkdir /etc/varnish
[root@linux ~]# uuidgen > /etc/varnish/secret
[root@linux ~]# cp /usr/local/varnish/etc/default.v cl /etc/varnish/
[root@linux ~]# vim /etc/varnish/default.vcl
[root@linux ~]# backend default {
.host = “192.168.139.10”;
.port = “80”;
}
启动variable。
[root@linux ~]# /etc/init.d/varnish start
客户端测试。
http://192.168.101.9/hello/
其他操作。
查看日志文件,方便以后进行调优。
[root@linux ~]# varnishncsa
192.168.101.6 - - [19/Jan/2019:15:03:08 +0800] “GET http://192.168.101.9/hello/ HTTP/1.1” 200 12 “-” “curl/7.29.0”
…
更新缓存数据,在后台web服务器更新页面内容后,用户访问代理服务器看到的还是之前
的数据,说明缓存中的数据过期了需要更新(默认也会自动更新,但非实时更新)。
varnishadm – S /etc/varnish/secret – T 127.0.0.1:6082 ban.url 页面文件