企业1 linux--varnish,进行cdn的简单实验

1##########安装varnishi所需软件包

server1:
[root@server1 ~]# yum install -y varnish-4.0.5-1.el7.x86_64.rpm varnish-libs-4.0.5-1.el7.x86_64.rpm jemalloc-3.6.0-1.el7.x86_64.rpm ##解决依赖性
[root@server1 ~]# rpm -qa | grep varnish  #已经安装
varnish-4.0.5-1.el7.x86_64
varnish-libs-4.0.5-1.el7.x86_64
[root@server1 ~]# rpm -qc varnish-4.0.5-1.el7.x86_64   #配置文件
/etc/logrotate.d/varnish
/etc/varnish/default.vcl
/etc/varnish/varnish.params


在这里插入图片描述
##########更改最大文件数

[root@server1 ~]# vim /usr/lib/systemd/system/varnish.service    #配置文件
[root@server1 ~]# ulimit -n                       #
1024
[root@server1 ~]# 
[root@server1 ~]# sysctl -a | grep file       #最大开启文件数
fs.file-max = 97311
fs.file-nr = 3872 0 97311
fs.xfs.filestream_centisecs = 3000
[root@server1 ~]# 
##################################################################picture  本纪最大开启文件数1
[root@server1 ~]# sysctl -a | grep file
fs.file-max = 199693
fs.file-nr = 4064 0 199693
fs.xfs.filestream_centisecs = 3000
[root@server1 ~]# ^C
[root@server1 ~]# 

在这里插入图片描述

##用户的限制文件(与varnishi配置文件中限制相对应)


[root@server1 ~]# vim /etc/security/limits.conf 
# End of file
varnish        -     nofile              131072
varnish        -     memlock             82000[root@server1 ~]# sysctl -a | grep file
-- 插入 --                                                                                     63,86        底端


在这里插入图片描述
##更改端口

[root@server1 ~]# vim /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80
[root@server1 ~]# systemctl start varnish
[root@server1 ~]# ps aux | grep varnish
root      1690  0.1  4.2 124868 87856 ?        SLs  18:08   0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :80 -T 127.0.0.1:6082 -S /etc/varnish/secret -u varnish -g varnish -s malloc,256M
varnish   1693  0.1  4.5 271772 93048 ?        Sl   18:08   0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :80 -T 127.0.0.1:6082 -S /etc/varnish/secret -u varnish -g varnish -s malloc,256M
root      1914  0.0  0.0 112664   972 pts/0    R+   18:09   0:00 grep --color=auto varnish
[root@server1 ~]# 




在这里插入图片描述

##更改配置文件中指定后端服务器:


[root@server1 ~]# vim /etc/varnish/default.vcl 
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "172.25.254.1";
    .port = "80";
}
sub vcl_recv {
[root@server1 ~]# systemctl restart varnish
[root@server1 ~]# netstat -tnlp
tcp6       0      0 :::80                   :::*                    LISTEN      2103/varnishd

###后端中

[root@server2 ~]# yum install -y httpd
[root@server2 ~]# vim /var/www/html/index.html
server2.example.com
[root@server2 ~]# systemctl start httpd
##测试
物理机中
[root@foundation71 第一天]# curl 172.25.254.1
server2.example.com
[root@foundation71 第一天]# curl 172.25.254.1
server2.example.com
[root@foundation71 第一天]# curl 172.25.254.1
server2.example.com
2、##############缓存情况varnish
[root@server1 ~]# vim /etc/varnish/default.vcl
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);
}
[root@server1 ~]# systemctl restart varnish
[root@server1 ~]# varnishadm ban req.url "~" /    #清除缓存


#####物理机中


[root@foundation71 第一天]# curl -I 172.25.254.1
HTTP/1.1 200 OK
Date: Mon, 16 Sep 2019 10:35:11 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Mon, 16 Sep 2019 10:18:14 GMT
ETag: "14-592a8ebf1e1a0"
Content-Length: 20
Content-Type: text/html; charset=UTF-8
X-Varnish: 32774
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache  #没有缓存时
Connection: keep-alive
[root@foundation71 第一天]# curl -I 172.25.254.1
HTTP/1.1 200 OK
Date: Mon, 16 Sep 2019 10:35:11 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Mon, 16 Sep 2019 10:18:14 GMT
ETag: "14-592a8ebf1e1a0"
Content-Length: 20
Content-Type: text/html; charset=UTF-8
X-Varnish: 7 32775
Age: 14
Via: 1.1 varnish-v4
X-Cache: HIT from westos cache       #有缓存时
Connection: keep-alive

在这里插入图片描述
##在重新加上一个后端服务器,不同域名站点的后端服务器。在一个ip下有多个域名


[root@server3 ~]# yum install httpd -y
[root@server3 ~]# systemctl start httpd
[root@server3 ~]# vim /var/www/html/index.html
server3.example.com

server1:在配置文件中设置:
backend web1 {
    .host = "172.25.254.2";
    .port = "80";
}
backend web2 {
    .host = "172.25.254.3";
    .port = "80";
}


sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = web1;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
} else {return (synth(405));
}
}


在这里插入图片描述


[root@server1 ~]# systemctl restart varnish
#测试
[root@foundation71 第一天]# vim /etc/hosts
172.25.254.1  server1  www.westos.org   bbs.westos.org
172.25.254.2  server2
172.25.254.3  server3
172.25.254.4  server4
172.25.254.100
[root@foundation71 第一天]# curl www.westos.org
server2.example.com
[root@foundation71 第一天]# curl bbs.westos.org
server3.example.com
[root@foundation71 第一天]# curl www.westos.org
server2.example.com
[root@foundation71 第一天]# curl bbs.westos.org
server3.example.com



在这里插入图片描述

###论询机制实现,后端服务器的负载均衡


server1:
[root@server1 ~]# vim /etc/varnish/default.vcl 
import directors from "/usr/lib64/varnish/vmods/libvmod_directors.so";
sub vcl_init {
    new lb = directors.round_robin();
    lb.add_backend(web1);
    lb.add_backend(web2);
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend_hint = lb.backend() ;
return(pass);
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend_hint = web2;
} else {return (synth(405));
}


[root@server1 ~]# systemctl restart varnish

在这里插入图片描述
##测试

[root@foundation71 第一天]# curl www.westos.org
server2.example.com
[root@foundation71 第一天]# curl www.westos.org
server3.example.com
[root@foundation71 第一天]# curl www.westos.org
server2.example.com
[root@foundation71 第一天]# curl www.westos.org
server3.example.com
[root@foundation71 第一天]# curl www.westos.org
server2.example.com
[root@foundation71 第一天]# curl www.westos.org
server3.example.com
[root@foundation71 第一天]#

####当采用轮询时,两个域名都会访问server3,无法时别,所以可以通过创建虚拟主机的方法来让不同的域名访问不同的内容。访问同一个ip由于访问时通过的域名不一样,而导致内容不一致。


server3:
 17  vim vhost.conf
   18  mkdir /www
   19  mkdir /bbs
   20  vim /www/index.html

[root@server3 conf.d]# cd /etc/httpd/conf.d/
[root@server3 conf.d]# cat /www/index.html
server3-www-vhost.example.com
[root@server3 conf.d]# cat /bbs/index.html
server-bbs-vhost.example.com
[root@server3 conf.d]# vim vhost.conf
<VirtualHost *:80>
        DocumentRoot     /www
        ServerName       www.westos.org
</VirtualHost>
<Directory "/www">
        Require all granted
</Directory>



<VirtualHost *:80>
        DocumentRoot     /bbs
        ServerName       bbs.westos.org
</VirtualHost>
<Directory "/bbs">
        Require all granted
</Directory>
[root@server3 conf.d]#    31  systemctl restart httpd
bash: 31: 未找到命令...
[root@server3 conf.d]# systemctl restart httpd
##物理机测试
[root@foundation71 第一天]# curl www.westos.org
server2.example.com
[root@foundation71 第一天]# curl www.westos.org
server3-www-vhost.example.com
[root@foundation71 第一天]# curl bbs.westos.org
server-bbs-vhost.example.com
[root@foundation71 第一天]# 


在这里插入图片描述
在这里插入图片描述

##推动平台的创建

[root@server1 ~]# unzip bansys.zip -d /var/www/html/
[root@server1 html]# ls
bansys
[root@server1 html]# mv bansys/* /var/www/html/
[root@server1 html]# ls
bansys  class_socket.php  config.php  index.php  purge_action.php  static
[root@server1 html]# rm -rf bansys/


[root@server1 html]# vim config.php 
<?php
 //varnish主机列表
 //可定义多个主机列表
 $var_group1 = array(
                        'host' => array('172.25.254.1'),
                                                'port' => '8080',
                    );


//varnish群组定义
 //对主机列表进行绑定
 $VAR_CLUSTER = array(
                         'www.westos.org' => $var_group1,
                     );
//varnish版本
 //2.x和3.x推送命令不一样
 $VAR_VERSION = "3";

?>
[root@server1 html]# systemctl restart httpd



在这里插入图片描述

@foundation71 第一天]# curl -I www.westos.org
HTTP/1.1 200 OK
Date: Mon, 16 Sep 2019 15:20:53 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Mon, 16 Sep 2019 14:29:02 GMT
ETag: "1e-592ac6cdd1a48"
Accept-Ranges: bytes
Content-Length: 30
Content-Type: text/html; charset=UTF-8
X-Varnish: 29
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache
Connection: keep-alive

[root@foundation71 第一天]# curl -I www.westos.org
HTTP/1.1 200 OK
Date: Mon, 16 Sep 2019 15:22:21 GMT
Server: Apache/2.4.6 (Red Hat Enterprise Linux)
Last-Modified: Mon, 16 Sep 2019 10:18:14 GMT
ETag: "14-592a8ebf1e1a0"
Accept-Ranges: bytes
Content-Length: 20
Content-Type: text/html; charset=UTF-8
X-Varnish: 32794
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS from westos cache
Connection: keep-alive

##############################客户端进行缓存清理

bansys 有两种工作模式,分别是:telnet 和 http 模式。
if (req.method == "BAN") {
if (!client.ip ~ westos) {
return(synth(405,"Not allowed"));
}
ban("req.url ~ " + req.url);
return(purge);
}
}
[root@server1 html]# systemctl restart varnish


在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值