varnish

简介:
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 images]# 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 images]# 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 images]# 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

 












本文转自blueclo51CTO博客,原文链接: http://blog.51cto.com/12774272/1949537,如需转载请自行联系原作者


相关文章
|
人工智能 前端开发 Devops
.NET技术在现代开发中的作用:.NET技术的核心价值、在现代应用开发中的实际应用、以及面临的挑战与未来趋势。
.NET技术是软件开发领域的核心力量,本文从其核心价值、实际应用及未来挑战三方面进行探讨。它支持多种语言,提供强大的开发工具和丰富的类库,并具备跨平台能力。在现代应用开发中,.NET广泛应用于企业级系统、Web应用、移动应用、云服务和游戏开发等领域。面对性能优化、容器化、AI集成等挑战,.NET持续创新以适应不断发展变化的技术环境。
323 4
|
9月前
|
机器学习/深度学习 人工智能 自然语言处理
PAI Model Gallery 支持云上一键部署 DeepSeek-V3、DeepSeek-R1 系列模型
DeepSeek 系列模型以其卓越性能在全球范围内备受瞩目,多次评测中表现优异,性能接近甚至超越国际顶尖闭源模型(如OpenAI的GPT-4、Claude-3.5-Sonnet等)。企业用户和开发者可使用 PAI 平台一键部署 DeepSeek 系列模型,实现 DeepSeek 系列模型与现有业务的高效融合。
|
11月前
|
机器学习/深度学习 人工智能 搜索推荐
AI技术在医疗领域的应用与前景
本文探讨了人工智能(AI)技术在医疗领域的应用,包括疾病诊断、治疗方案制定、药物研发等方面。通过对现有研究成果的梳理,分析了AI技术在提高医疗服务效率、降低医疗成本、改善患者体验等方面的潜力。同时,也指出了AI技术在医疗领域面临的挑战,如数据隐私保护、伦理道德问题等,并展望了未来的发展趋势。
945 2
|
11月前
|
机器学习/深度学习 人工智能 计算机视觉
深度学习在医疗影像分析中的应用与挑战
本文探讨了深度学习技术在医疗影像分析领域的应用现状和面临的主要挑战。随着人工智能技术的飞速发展,深度学习已经成为推动医疗影像诊断自动化和智能化的重要力量。文章首先概述了深度学习的基本原理及其在图像识别任务中的优势,随后详细讨论了其在CT、MRI等医疗影像处理中的成功案例,并分析了当前技术面临的数据隐私、模型解释性以及临床验证等方面的挑战。最后,提出了未来研究的方向和可能的解决方案,旨在促进深度学习技术在医疗领域的更广泛应用。
290 0
|
虚拟化
VMware Workstation子网划分及bond配置案例
本文主要介绍了如何在VMware Workstation中进行子网划分以及如何添加和配置四块网卡以实现bond模式,并通过配置文件和命令行操作来验证bond模式的有效性。
321 3
VMware Workstation子网划分及bond配置案例
|
机器学习/深度学习 人工智能 自然语言处理
【AI大模型】LLM主流开源大模型介绍
【AI大模型】LLM主流开源大模型介绍
|
安全 API 网络架构
Python中哪个框架最适合做API?
本文介绍了Python生态系统中几个流行的API框架,包括Flask、FastAPI、Django Rest Framework(DRF)、Falcon和Tornado。每个框架都有其独特的优势和适用场景。Flask轻量灵活,适合小型项目;FastAPI高性能且自动生成文档,适合需要高吞吐量的API;DRF功能强大,适合复杂应用;Falcon高性能低延迟,适合快速API开发;Tornado异步非阻塞,适合高并发场景。文章通过示例代码和优缺点分析,帮助开发者根据项目需求选择合适的框架。
2254 0
|
JavaScript API
VUE——监听浏览器关闭及标签页关闭事件
VUE——监听浏览器关闭及标签页关闭事件
802 0
|
机器学习/深度学习 人工智能 自然语言处理
人工智能在当代社会中的应用与未来发展趋势 摘要:
人工智能(AI)作为一种新兴技术,在当代社会中扮演着越来越重要的角色。本文将探讨人工智能的基本概念、当前在各个领域的应用情况以及未来的发展趋势。首先,我们将介绍人工智能的定义和分类,然后详细讨论人工智能在医疗、金融、制造业、交通、教育等领域的应用案例。接着,我们将分析人工智能发展面临的挑战,包括数据隐私、伦理道德等问题,并探讨如何解决这些问题以推动人工智能的发展。最后,我们将展望人工智能的未来发展趋势,包括深度学习、自然语言处理、机器人技术等方面的进展,以及人工智能对社会和经济的影响。
2146 1
|
SQL 关系型数据库 MySQL
mysql和polardb
mysql和polardb
3963 2