####varnish####
三台虚拟机:iptables,selinux关闭

#1.varnish配置

##server1上:
1.安装varnish
yum install -y varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm

2.配置文件
rpm -qc varnish-3.0.5-1.el6.x86_64
/etc/logrotate.d/varnish
/etc/sysconfig/varnish
/etc/varnish/default.vcl
1) vim /etc/sysconfig/varnish
NFILES=131072 ##最大打开文件个数,sysctl -a | grep file ,看到最大文件数没达到131072,所以内存增加到2048,或者这个值改为65535(file-max是设置 系统所有进程一共可以打开的文件数量)

MEMLOCK=82000 ##内存锁定(就是运行varnish需要82M内存空间,但是系统只允许64M,所以也要修改)

NPROCS=“unlimited” ##进程数,varnish要求不限制,所以要修改系统的限制

VARNISH_LISTEN_PORT=80 ##让varnish的默认端口为80,在http请求时进行缓存或调度

2)vim /etc/security/limits.conf #修改系统参数

varnish         -       nofile          131072
varnish         -       memlock         82000
varnish         -       nproc           unlimited

##varnish服务##

##配置一个后端服务器
3) vim /etc/varnish/default.vcl

backend default {
  .host = "172.25.0.2";	##配置后端realserver
  .port = "80";
}

##varnish服务##

#在server2上:
yum install -y httpd
vim /var/www/html/index.html
server3
##varnish服务##
/etc/init.d/httpd start

测试:
在物理机上 测试:
在物理机上 curl 172.25.1.2 , 看到访问的是server3

##varnish服务##

####查看缓存命中情况####
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);
}

##varnish服务##
/etc/init.d/varnish reload

VARNISH_THREAD_TIMEOUT=120	        ##120s刷新

测试:
再次 curl 172.25.0.1 , 第一次未命中缓存,后面都会命中

#手动清除缓存
varnishadm ban.url .*$ ##清除所有
varnishadm ban.url /index.html ##清除index.html页面缓存

####定义不同域名站点的后端服务器####
再启一台虚拟机,安装httpd , 写index.html --> bbs.westos.org
在物理机上写好本地解析

vim /etc/varnish/default.vcl

backend web1 {
  .host = "172.25.0.2";
  .port = "80";
}

backend web2 {
  .host = "172.25.0.3";
  .port = "80";
}

sub vcl_recv {
   if (req.http.host ~ "^(www.)?westos.org") {
      set req.http.host = "www.westos.org";
      set req.backend = web1;
} elsif (req.http.host ~ "^bbs.westos.org") {
      set req.backend = web2;
   } else {
      error 404 "westos cache";
     }
}

##varnish服务##

/etc/init.d/varnish reload

测试:
##varnish服务##

再访问时,不同域名对应不同主机

####定义负载均衡####

在web2中定义虚拟主机

mkdir /www
cd /www
vim index.html
mkdir /bbs
cd /bbs
vim index.html

vim /etc/http/conf/httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /www
    ServerName www.westos.org
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /bbs
    Servername bbs.westos.org
</VirtualHost>

##varnish服务##

vim /etc/varnish/default.vcl

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;			##当访问www.westos.org时,请求转到lb
        #return (pass);					##测试时可打开此选项,不进行缓存
    } elsif (req.http.host ~ "^bbs.westos.org") {
        set req.backend = web2;
    } else {
        error 404 "westos cache";
    }
}

##varnish服务##
/etc/init.d/varnish reload

测试(手动清缓存,或者加上不缓存的配置):

##varnish服务##

###排错思想###

如果所有操作都做完了但仍然没有出现正确的现象该怎么办?

此时需要检查:

火墙是否关闭 /etc/init.d/iptables stop

apache是否打开 /etc/init.d/httpd restart

varnish服务是否重启 /etc/init.d/varnish restart

varnish的重新加载 /etc/init.d/varnish reload

配置文件是否编写正确
每提交一次就会更改一次

相关文章:

  • 2021-07-23
  • 2022-12-23
  • 2021-04-02
  • 2021-11-20
  • 2021-11-06
  • 2021-12-28
  • 2021-04-17
猜你喜欢
  • 2021-09-13
  • 2021-11-03
  • 2021-09-13
  • 2022-01-09
  • 2021-04-25
  • 2021-05-17
  • 2021-09-01
相关资源
相似解决方案