varnish

可以提高网页的访问速度,提高服务器系统,降低访问压力,客户端访问时,dns其实是解析到varnish,然后,如果varnish里有客户端所需要的内容,则将内容给客户端,如果没有,则varnish到后端服务器去取来发给客户端的同时,还会缓存一份,这样,如果有别的客户端要访问就可以通过缓存快速获取到
一 主机环境
安装rhel6.5的虚拟机 --> 通过网络安装 --> URL --> Memory(rhel6.5版本最小为512M,如果比这个小虽然能安装但是会出现问题,由于主机内存问题,所以安装最小的512M) -- > 硬盘 -- > 虚拟机名字
虚拟机安装过程选项:
安装过程的语言:
键盘:
ip4:
re-initialize all:
超级用户的密码:
####封装#####
1 hostname server1
2 vi /etc/sysconfig/network        ###修改主机名###
内容:
NETWORK = yes
HOSTNAME = server1
vi /etc/yum.repos.d/rhel-source.repo    ###修改yum仓库###
内容:
baseurl = http://172.25.254.78/rhel6.5
enable = 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    restart
cd /etc/ssh/
rm -fr ssh_host_*
/etc/init.d/sshd  restart
rm -f /etc/sysconfig/iptables        ###删除防火墙策略###
chkconfig  iptables  off        ###关闭防火墙###
vim /ect/sysconfig/selinux        ###关闭selinux
vim /etc/hosts
poweroff
####建立快照####
virsh undefine base        删除vm的前端管理,不会删除存储
[root@foundation78 p_w_picpaths]# qemu-img  create -f qcow2 -b base.qcow2 vm1
Formatting '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 vm2
Formatting '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 vm3
Formatting '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.rpm
varnish-libs-3.0.5-1.el6.x86_64.rpm
2 配置文件
主配置文件:/etc/sysconfig/varnish
子配置文件:/etc/varnish/default.vcl
1)为了访问的方便,将varnish的端口该成80,这样输入网址即可访问到varnish
在/etc/sysconfig/varnish修改:
 66 VARNISH_LISTEN_PORT=80
2)指定后端:访问172.25.78.2的80端口
vim /etc/varnish/default.vcl
backend 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 reload
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2017-07-19T10:02:29
VCL compiled.
available       0 boot
active          0 reload_2017-07-19T10:02:29
Done
###在后端服务器server2的操作###
1 yum install -y httpd
2  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 restart
Stopping 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 start
Starting httpd:
[root@server2 ~]# /etc/init.d/httpd restart
Stopping 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.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 02:07:02 GMT
ETag: "8d2-11-554a216807226"
Content-Type: text/html; charset=UTF-8
Content-Length: 17
Accept-Ranges: bytes
Date: Wed, 19 Jul 2017 02:13:06 GMT
X-Varnish: 1793081648 1793081642
Age: 119                ###缓存时间,超过120s就会清除缓存重新到后端获取###
Via: 1.1 varnish
Connection: keep-alive
X-Cache: HIT from westos cache        ###缓存命中,说明缓存中有###
[root@foundation78 iso]# curl -I 172.25.78.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 02:07:02 GMT
ETag: "8d2-11-554a216807226"
Content-Type: text/html; charset=UTF-8
Content-Length: 17
Accept-Ranges: bytes
Date: Wed, 19 Jul 2017 02:13:07 GMT
X-Varnish: 1793081649
Age: 0                    
Via: 1.1 varnish
Connection: keep-alive
X-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.1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 02:07:02 GMT
ETag: "8d2-11-554a216807226"
Content-Type: text/html; charset=UTF-8
Content-Length: 17
Accept-Ranges: bytes
Date: Wed, 19 Jul 2017 02:30:34 GMT
X-Varnish: 1793081653
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Cache: MISS from westos cache
[root@foundation78 iso]# curl -I www.westos.org
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
Last-Modified: Wed, 19 Jul 2017 02:07:02 GMT
ETag: "8d2-11-554a216807226"
Content-Type: text/html; charset=UTF-8
Content-Length: 17
Accept-Ranges: bytes
Date: Wed, 19 Jul 2017 02:32:27 GMT
X-Varnish: 1793081654
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-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 reload
3 server3的配置
yum install -y httpd
/etc/init.d/httpd start
vim /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.vcl
backend 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 reload
server2 端的配置:
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 restart
3 mkdir /www/bbs -p
  mkdir /www/westos    
4 vim /www/bbs/index.html
内容:
bbs.westos.org
5 vim /www/westos/index.html
内容:
server2-www.westos.com
6 /etc/init.d/httpd restart
server3的配置:
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.org
server2-www.westos.com
[root@foundation78 iso]# curl bbs.westos.org
bbs.westos.org
varnish 推送:
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.zip
2 yum install -y unzip
3 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/html
6 mv /bansys/* /var/www/html        ###可以直接访问index.php
7 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  start
Starting httpd:
[root@server1 ~]# /etc/init.d/httpd  restart
Stopping 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]# ls
bansys.zip
[root@server1 html]# yum install -y unzip
[root@server1 html]# ls
bansys.zip
[root@server1 html]# unzip bansys.zip   
[root@server1 html]# ls
bansys  bansys.zip
[root@server1 html]# mv bansys.zip /root/
[root@server1 html]# ls
bansys
[root@server1 html]# cd bansys/
[root@server1 bansys]# ls
class_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]# ls
bansys  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 -antlp
Active 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]# ls
bansys  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 restart
Stopping 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 reload
Loading vcl from /etc/varnish/default.vcl
Current running config name is boot
Using new config name reload_2017-07-19T09:51:49
VCL compiled.
available       2 boot
active          0 reload_2017-07-19T09:51:49
Done