varnish
可以提高网页的访问速度,提高服务器系统,降低访问压力,客户端访问时,dns其实是解析到varnish,然后,如果varnish里有客户端所需要的内容,则将内容给客户端,如果没有,则varnish到后端服务器去取来发给客户端的同时,还会缓存一份,这样,如果有别的客户端要访问就可以通过缓存快速获取到一 主机环境安装rhel6.5的虚拟机 --> 通过网络安装 --> URL --> Memory(rhel6.5版本最小为512M,如果比这个小虽然能安装但是会出现问题,由于主机内存问题,所以安装最小的512M) -- > 硬盘 -- > 虚拟机名字虚拟机安装过程选项:安装过程的语言:键盘:ip4:re-initialize all:超级用户的密码:####封装#####1 hostname server12 vi /etc/sysconfig/network ###修改主机名###内容:NETWORK = yesHOSTNAME = server1vi /etc/yum.repos.d/rhel-source.repo ###修改yum仓库###内容:baseurl = http://172.25.254.78/rhel6.5enable = 1 ###激活仓库###yum install -y vim lftp openssh-clients cd /etc/udev/rules.d/rm -f 70-persistent-net.rules ###这个文件会将eth0占用着,导致下一次启动时,由于物理地址的改变会使得没有网卡分配,而无法启动###vim /etc/sysconfig/network-scripts/ifcfg-eth0/etc/init.d/network restartcd /etc/ssh/rm -fr ssh_host_*/etc/init.d/sshd restartrm -f /etc/sysconfig/iptables ###删除防火墙策略###chkconfig iptables off ###关闭防火墙###vim /ect/sysconfig/selinux ###关闭selinuxvim /etc/hostspoweroff####建立快照####virsh undefine base 删除vm的前端管理,不会删除存储[root@foundation78 p_w_picpaths]# qemu-img create -f qcow2 -b base.qcow2 vm1Formatting 'vm1', fmt=qcow2 size=21474836480 backing_file='base.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off [root@foundation78 p_w_picpaths]# qemu-img create -f qcow2 -b base.qcow2 vm2Formatting 'vm2', fmt=qcow2 size=21474836480 backing_file='base.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off [root@foundation78 p_w_picpaths]# qemu-img create -f qcow2 -b base.qcow2 vm3Formatting 'vm3', fmt=qcow2 size=21474836480 backing_file='base.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off vim /etc/sysconfig/network-scripts/ifcfg-eth0二 varnish配置###将server1作为varnish###1 安装包:varnish-3.0.5-1.el6.x86_64.rpmvarnish-libs-3.0.5-1.el6.x86_64.rpm2 配置文件主配置文件:/etc/sysconfig/varnish子配置文件:/etc/varnish/default.vcl1)为了访问的方便,将varnish的端口该成80,这样输入网址即可访问到varnish在/etc/sysconfig/varnish修改: 66 VARNISH_LISTEN_PORT=802)指定后端:访问172.25.78.2的80端口vim /etc/varnish/default.vclbackend default { .host = "172.25.78.2"; ###要访问的后端服务器的ip## .port = "80"; ###端口###}###查看缓存命中情况sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from westos cache";}else { set resp.http.X-Cache = "MISS from westos cache";}return (deliver);}3 reload varnish服务[root@server1 varnish]# /etc/init.d/varnish start[root@server1 varnish]# /etc/init.d/varnish reloadLoading vcl from /etc/varnish/default.vclCurrent running config name is bootUsing new config name reload_2017-07-19T10:02:29VCL compiled.available 0 bootactive 0 reload_2017-07-19T10:02:29Done###在后端服务器server2的操作###1 yum install -y httpd2 vim /var/www/html/index.html内容:<h1>server2</h1>~ 3 /etc/init.d/httpd start ###开启服务### 4 /etc/init.d/httpd restart ###重启服务###过程如下:[root@server2 ~]# vim /var/www/html/index.html[root@server2 ~]# /etc/init.d/httpd restartStopping httpd: [FAILED]Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.78.2 for ServerName [ OK ][root@server2 ~]# /etc/init.d/httpd startStarting httpd: [root@server2 ~]# /etc/init.d/httpd restartStopping httpd: [ OK ]Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.78.2 for ServerName [ OK ][root@server2 ~]# vim /etc/yum.repos.d/rhel-source.repo [root@server2 ~]# vim /var/www/html/index.html 测试:用172.25.254.78访问varnish主机即server1主机,查看结果###测试缓存命中###[root@foundation78 iso]# curl -I 172.25.78.1HTTP/1.1 200 OKServer: Apache/2.2.15 (Red Hat)Last-Modified: Wed, 19 Jul 2017 02:07:02 GMTETag: "8d2-11-554a216807226"Content-Type: text/html; charset=UTF-8Content-Length: 17Accept-Ranges: bytesDate: Wed, 19 Jul 2017 02:13:06 GMTX-Varnish: 1793081648 1793081642Age: 119 ###缓存时间,超过120s就会清除缓存重新到后端获取###Via: 1.1 varnishConnection: keep-aliveX-Cache: HIT from westos cache ###缓存命中,说明缓存中有###[root@foundation78 iso]# curl -I 172.25.78.1HTTP/1.1 200 OKServer: Apache/2.2.15 (Red Hat)Last-Modified: Wed, 19 Jul 2017 02:07:02 GMTETag: "8d2-11-554a216807226"Content-Type: text/html; charset=UTF-8Content-Length: 17Accept-Ranges: bytesDate: Wed, 19 Jul 2017 02:13:07 GMTX-Varnish: 1793081649Age: 0 Via: 1.1 varnishConnection: keep-aliveX-Cache: MISS from westos cache ###超过了120s,缓存被清除了,所以缓存中没有,要到后端服务器去取######用curl命令查看server1主机访问到的内容###[root@foundation78 iso]# vim /etc/hosts内容:72.25.78.1 server1 www.westos.org bbs.westos.com westos.com[root@foundation78 iso]# curl www.westos.org<h1>server2</h1>[root@foundation78 iso]# 三 通过varnishadm 手动清除缓存varnishadm ban.url .*$ #清除所有varnishadm ban.url /index.html #清除 index.html 页面缓存varnishadm ban.url /admin/$ #清除 admin 目录缓存过程如下:###varnish端###[root@server1 varnish]# varnishadm ban.url .*$[root@server1 varnish]# [root@server1 varnish]# varnishadm ban.url www.westos.org/index.html[root@server1 varnish]# ###访问端###[root@foundation78 iso]# curl -I 172.25.78.1HTTP/1.1 200 OKServer: Apache/2.2.15 (Red Hat)Last-Modified: Wed, 19 Jul 2017 02:07:02 GMTETag: "8d2-11-554a216807226"Content-Type: text/html; charset=UTF-8Content-Length: 17Accept-Ranges: bytesDate: Wed, 19 Jul 2017 02:30:34 GMTX-Varnish: 1793081653Age: 0Via: 1.1 varnishConnection: keep-aliveX-Cache: MISS from westos cache[root@foundation78 iso]# curl -I www.westos.orgHTTP/1.1 200 OKServer: Apache/2.2.15 (Red Hat)Last-Modified: Wed, 19 Jul 2017 02:07:02 GMTETag: "8d2-11-554a216807226"Content-Type: text/html; charset=UTF-8Content-Length: 17Accept-Ranges: bytesDate: Wed, 19 Jul 2017 02:32:27 GMTX-Varnish: 1793081654Age: 0Via: 1.1 varnishConnection: keep-aliveX-Cache: MISS from westos cache四 定义多个不同域名站点的后端服务器 1 vim /etc/varnish/default.vcl内容如下:backend web1 { ###web1只是一个别名### .host = "172.25.78.2"; .port = "80";}backend web2 { .host = "172.25.78.3"; .port = "80";}###该语句块是所有varnish的入口,会读取该语句块的内容###sub vcl_recv { if (req.http.host ~ "^(www.)?westos.org") { ###不论访问是www.westos.org还是westos.org都将其变成www.westos.org###set req.http.host = "www.westos.org";set req.backend = web1; ###访问www.westos.org的varnish的后端服务器是web1即server2###} elsif (req.http.host ~ "^bbs.westos.org") { set req.backend = web2; ###访问bbs.westos.org的varnish的后端服务器是web2即server3} else {error 404 "westos cache"; ###除此之外的访问均404报错###}}2 /etc/init.d/varnish reload3 server3的配置yum install -y httpd/etc/init.d/httpd startvim /var/www/html/index.html内容:<h1>server2</h1>~ /etc/init.d/httpd restart测试:测试端:1 vim /etc/hosts ##域名解析###172.25.78.1 server1 www.westos.org bbs.westos.org westos.org过程如下:[root@foundation78 iso]# vim /etc/hosts[root@foundation78 iso]# curl www.westos.org<h1>server2</h1>[root@foundation78 iso]# curl westos.org<h1>server2</h1>[root@foundation78 iso]# curl bbs.westos.org<h1>server3</h1>用172.25.254.78访问172.25.78.1 五 通过varnish实现负载均衡varnish端的配置:1 vim /etc/varnish/default.vclbackend web1 { .host = "172.25.78.2"; .port = "80";}backend web2 { .host = "172.25.78.3"; .port = "80";}director lb round-robin { ###把后端聚成一个组###{.backend = web1;}{.backend = web2;}}sub vcl_recv { if (req.http.host ~ "^(www.)?westos.org") { set req.http.host = "www.westos.org";set req.backend = lb;return(pass); ###为了更方便看到效果,使用该语句可以使得varnish不缓存###} elsif (req.http.host ~ "^bbs.westos.org") { set req.backend = web1;} else {error 404 "westos cache";}}2 /etc/init.d/varnish reloadserver2 端的配置:1 vim /etc/httpd/conf/httpd.conf NameVirtualHost *:80 ###激活虚拟主机###<VirtualHost *:80> DocumentRoot /var/www/html ServerName server2</VirtualHost><VirtualHost *:80> DocumentRoot /www/bbs ServerName bbs.westos.org</VirtualHost><VirtualHost *:80> DocumentRoot /www/westos ServerName www.westos.org</VirtualHost>2 /etc/init.d/httpd restart3 mkdir /www/bbs -p mkdir /www/westos 4 vim /www/bbs/index.html内容:bbs.westos.org5 vim /www/westos/index.html内容:server2-www.westos.com6 /etc/init.d/httpd restartserver3的配置:1 vim /var/www/html/index.html内容:<h1>server3-www.westos.org</h1>2 /etc/init.d/httpd restart测试:[root@foundation78 iso]# curl www.westos.org<h1>server3-www.westos.org</h1>[root@foundation78 iso]# curl www.westos.orgserver2-www.westos.com[root@foundation78 iso]# curl bbs.westos.orgbbs.westos.orgvarnish 推送:varnish端:1 vim /etc/varnish/default.vcl内容:acl westos { ####访问控制列表,只有本机和172.25.38.0网段的用户可以进行varnish推送###"127.0.0.1"; "172.25.38.0"/24;}sub vcl_recv { if (req.request == "BAN") { if (!client.ip ~ westos) { error 405 "Not allowed.";}ban("req.url ~ " + req.url);error 200 "ban added";}}2 /etc/init.d/varnish reload ###varnish cdn推送平台###1 从网上下载:http://code.google.com/p/varnish-php-bansys/下载bansys.zip2 yum install -y unzip3 yum install -y httpd 4 vim /etc/httpd/conf/httpd.conf内容:Listen 8080 ###由于推送平台和varnish是在一个主机上,而varnish使用了80端口,为了避免端口冲突,将http的端口改成8080,不然http服务不能正常开启###5 unzip bansys.zip -d /var/www/html6 mv /bansys/* /var/www/html ###可以直接访问index.php7 yum install php -y ###因为是用php写的,所以要安装php####8 vim /var/www/html/config.php 内容://varnish主机列表 //可定义多个主机列表 $var_group1 = array( 'host' => array('172.25.78.1'), 'port' => '80', ); //varnish群组定义 //对主机列表进行绑定 $VAR_CLUSTER = array( 'www.westos.org' => $var_group1, );过程如下:[root@server1 ~]# yum install -y httpd [root@server1 ~]# vim /etc/httpd/conf/httpd.conf内容:Listen 8080[root@server1 ~]# /etc/init.d/httpd startStarting httpd: [root@server1 ~]# /etc/init.d/httpd restartStopping httpd: [ OK ]Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.38.1 for ServerName [ OK ][root@server1 ~]# cd /var/www/html/[root@server1 html]# lsbansys.zip[root@server1 html]# yum install -y unzip[root@server1 html]# lsbansys.zip[root@server1 html]# unzip bansys.zip [root@server1 html]# lsbansys bansys.zip[root@server1 html]# mv bansys.zip /root/[root@server1 html]# lsbansys[root@server1 html]# cd bansys/[root@server1 bansys]# lsclass_socket.php config.php index.php purge_action.php static[root@server1 bansys]# mv * ..[root@server1 bansys]# ls[root@server1 bansys]# cd ..[root@server1 html]# lsbansys class_socket.php config.php index.php purge_action.php static[root@server1 html]# pwd/var/www/html[root@server1 html]# ll index.php -rw-r--r-- 1 root root 5410 Mar 24 2012 index.php[root@server1 html]# yum install php -y[root@server1 html]# netstat -antlpActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1122/varnishd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 920/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 997/master tcp 0 0 127.0.0.1:6082 0.0.0.0:* LISTEN 1121/varnishd tcp 0 0 172.25.38.1:22 172.25.38.250:39506 ESTABLISHED 1060/sshd tcp 0 0 172.25.38.1:38869 172.25.38.250:80 TIME_WAIT - tcp 0 0 :::8080 :::* LISTEN 1301/httpd tcp 0 0 :::80 :::* LISTEN 1122/varnishd tcp 0 0 :::22 :::* LISTEN 920/sshd tcp 0 0 ::1:25 :::* LISTEN 997/master [root@server1 html]# /etc/init.d/varnish start[root@server1 html]# vim config.php [root@server1 html]# lsbansys class_socket.php config.php index.php purge_action.php static[root@server1 html]# rm -fr bansys/[root@server1 html]# ll index.php -rw-r--r-- 1 root root 5410 Mar 24 2012 index.php[root@server1 html]# /etc/init.d/httpd restartStopping httpd: [ OK ]Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.38.1 for ServerName [ OK ][root@server1 html]# cd /etc/varnish/[root@server1 varnish]# vim default.vcl [root@server1 varnish]# /etc/init.d/varnish reloadLoading vcl from /etc/varnish/default.vclCurrent running config name is bootUsing new config name reload_2017-07-19T09:51:49VCL compiled.available 2 bootactive 0 reload_2017-07-19T09:51:49Done