Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群

实验环境:RHEL6.5

                实验环境4台

                真机进行访问测试     willis.example.com         172.25.254.6

                虚拟机1(缓存端)   varnish.example.com       172.25.254.8

                虚拟机2(服务器端1) web1.example.com     172.25.254.10

                虚拟机3(服务器端2) web2.example.com     172.25.254.20

                两台服务器主要用于负载均衡实验。

实验内容:1.服务器端安装Apache

                 2.代理端(缓存端)安装配置varnish

                 3.缓存无法同步问题

                 4.虚拟主机

                 5.网页重写

                 6.负载均衡器

安装包:varnish-3.0.5-1.el6.x86_64.rpm

             varnish-libs-3.0.5-1.el6.x86_64.rpm


1.服务器端安装Apache

    1.1 服务器端1

[root@web1 ~]# yum install httpd -y 

[root@web1 ~]# vim /var/www/html/index.html

                         web1's page    

[root@web1 ~]# /etc/init.d/httpd start

   1.2服务器端2

[root@web2 ~]# yum install httpd -y 

[root@web2 ~]# vim /var/www/html/index.html

                         web2's page    

[root@web2 ~]# /etc/init.d/httpd start


2.代理端(缓存端)安装配置varnish

[root@varnish mnt]# ls

        varnish-3.0.5-1.el6.x86_64.rpm  varnish-libs-3.0.5-1.el6.x86_64.rpm

[root@varnish mnt]# yum install * -y

[root@varnish mnt]# vim /etc/sysconfig/varnis

       VARNISH_LISTEN_PORT=80 ##设定varnish的端口为80

[root@varnish mnt]# vim /etc/varnish/default.vcl 

        backend web1 {

         .host = "172.25.254.10";      ##指定apache所在主机ip(本次实验服务器1/2端都可以)

          .port = "80";                ##apache端口

        }

[root@varnish mnt]# /etc/init.d/varnish start

        Starting Varnish Cache:                                    [  OK  ]

[root@varnish mnt]# vim /etc/varnish/default.vcl   ##设置缓存命中信息

sub vcl_deliver{

    if(obj.hits>0){

        set resp.http.X-Cache="HIT from willis cache";  ##缓存命中

        }

     else{

        set resp.http.X-Cache="MISS from willis cache";  ##缓存未命中

        }

    return(deliver);

    }

[root@varnish mnt]# service varnish reload

Loading vcl from /etc/varnish/default.vcl

Current running config name is boot

Using new config name reload_2016-09-15T05:19:54

VCL compiled.

available       0 boot

active          0 reload_2016-09-15T05:19:54

Done


3.缓存无法同步问题

    3.1 缓存端,访问缓存不过期页面无法刷新

[root@varnish ~]# curl http://172.25.254.8     

web1's page

[root@varnish ~]# curl -I http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:43:54 GMT

X-Varnish: 1470288177 1470288175

Age: 95

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache

[root@varnish ~]# curl -I http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:43:59 GMT

X-Varnish: 1470288178 1470288175

Age: 100

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache


 3.2 通过 varnishadm 手动清除缓存

# varnishadm ban.url .*$   #清除所有

# varnishadm ban.url /index.html  #清除 index.html 页面缓存

# varnishadm ban.url /admin/$ #清除 admin 目录缓存

[root@varnish mnt]# curl -I  http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:52:42 GMT

X-Varnish: 1470288192 1470288185

Age: 51

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache

[root@varnish mnt]# varnishadm ban.url /index.html

[root@varnish mnt]# curl -I  http://172.25.254.8

HTTP/1.1 200 OK

Server: Apache/2.2.15 (Red Hat)

Last-Modified: Wed, 14 Sep 2016 21:02:59 GMT

ETag: "1fcb4-c-53c7e0dd4191f"

Content-Type: text/html; charset=UTF-8

Content-Length: 12

Accept-Ranges: bytes

Date: Wed, 14 Sep 2016 21:53:24 GMT

X-Varnish: 1470288193 1470288185

Age: 93

Via: 1.1 varnish

Connection: keep-alive

X-Cache: HIT from willis cache


4.虚拟主机

    4.1服务端1设置

[root@web1 ~]# vim /etc/httpd/conf/httpd.conf 

<VirtualHost *:80>

    ServerAdmin /www/willis.com/html

    DocumentRoot www.willis.com

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin /www/linux.com/html

    DocumentRoot www.linux.com

</VirtualHost>

[root@web1 ~]# mkdir -p /www/willis.com/html

[root@web1 ~]# mkdir -p /www/linux.com/html

[root@web1 ~]# echo "web1 willis's page ">/www/willis.com/html/index.html

[root@web1 ~]# echo "web1 linux's page ">/www/linux.com/html/index.html

[root@web1 ~]# /etc/init.d/httpd restart

    4.2 服务端2设置

[root@web2 ~]# vim /etc/httpd/conf/httpd.conf 

<VirtualHost *:80>

    ServerAdmin /www/willis.com/html

    DocumentRoot www.willis.com

</VirtualHost>

<VirtualHost *:80>

    ServerAdmin /www/linux.com/html

    DocumentRoot www.linux.com

</VirtualHost>

[root@web2 ~]# mkdir -p /www/willis.com/html

[root@web2 ~]# mkdir -p /www/linux.com/html

[root@web2 ~]# echo "web2 willis's page ">/www/willis.com/html/index.html

[root@web2 ~]# echo "web2 linux's page ">/www/linux.com/html/index.html

[root@web2 ~]# /etc/init.d/httpd restart

    4.3访问端测试

[root@willis ~]# vim /etc/hosts

172.25.254.8      www.willis.com

172.25.254.8     www.linux.com

  ###注意,前面指定 .host = "172.25.254.10",所以访问结果为服务器1端的网页内容  


Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群

Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群



5.网页重写

[root@varnish mnt]# vim /etc/varnish/default.vcl 

backend web1 {

  .host = "172.25.254.10";

  .port = "80";

}

backend web2 {

   .host="172.25.254.20";

   .port="80";

}

sub vcl_recv { ##网页缓存       

   if (req.http.host ~ "^(www.)?willis.com" ) {      ##访问中是否带www

        set req.http.host = "www.willis.com";        ##都定向到www上

        set req.backend = web1;                  ##访问web1

        }

        elsif (req.http.host ~ "^(www.)?linux.com" ) {

        set req.http.host = "www.linux.com";

        set req.backend = web1;

        }

        else {error 404 "westos cache";

        }

}

[root@varnish mnt]# /etc/init.d/varnish restart

    访问端测试

   访问 willis.com自动跳转成www.willis.com

   访问 linux.com自动跳转成www.linux.com


Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群

Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群



6.负载均衡器

已经提前配好好两台服务器,本实验只需配置缓存端

[root@varnish mnt]# vim /etc/varnish/default.vcl 

backend web1 {

  .host = "172.25.254.10";

  .port = "80";

}

backend web2 {

   .host="172.25.254.20";

   .port="80";

}

director willislb round-robin {

        { .backend = web1; }

        { .backend = web2; }

}

sub vcl_recv {

        if (req.http.host ~ "^(www.)?westos.com" ) {

        set req.http.host = "www.westos.com";

        set req.backend = willislb;                 ##修改

       return (pass);                                ##方便测试,不缓存

        }

        elsif (req.http.host ~ "^(www.)?linux.com" ) {

        set req.http.host = "www.linux.com";

        set req.backend = willislb;                 ##修改

      return (pass);                                 ##方便测试,不缓存

        }

        else {error 404 "westos cache";

        }

}


访问端测试:

linux.com或者willis.com  网页重写,刷新可看到内容的变化


Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群

刷新:

Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群


Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群

刷新:

Apache+varnish(高性能开源HTTP加速器)搭建负载均衡集群



本文转自willis_sun 51CTO博客,原文链接:http://blog.51cto.com/willis/1852967,如需转载请自行联系原作者

上一篇:遇到的一个SCOM怪问题,虽然解决但是不知道为什么


下一篇:沪穗深百万地铁族担心:花生WiFi到底安全吗?