Varnish簡單案例

一、簡介及工作流程圖

    Varnish 是一款高性能且開源的反向代理服務器和 HTTP 加速器,其採用全新的軟件體系機構,和現在的硬件體系緊密配合,與傳統的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等諸多優點,

    varnish主要運行兩個進程:Management進程和Child進程(也叫Cache進程)。

        詳細內容請參看官網

官方提供工作流程圖: https://www.varnish-cache.org/

wKioL1Qnq93DCASnAAK-QIBEmPY295.jpg


二、實驗拓撲圖

wKioL1Qnt0rhuQx3AADoF9XdLTs048.jpg


三、安裝配置

[root@90sec src]# yum -y install varnish-docs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm 

1、查看生成了那些配置文件

[root@90sec src]# rpm -ql varnish

[root@90sec /]# vim /etc/sysconfig/varnish 

VARNISH_LISTEN_PORT=80                   #默認監聽爲6081

VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1   #管理的IP地址

VARNISH_ADMIN_LISTEN_PORT=6082           #管理端口

VARNISH_MIN_THREADS=50                   #最小線程數量

VARNISH_MAX_THREADS=1000                 #最大線程數量

VARNISH_THREAD_TIMEOUT=120               #線程超時時間

VARNISH_STORAGE_SIZE=1G                  #設置文件緩存大小變量

VARNISH_MEMORY_SIZE=80M                   #設置內存緩存大小變量

VARNISH_STORAGE="malloc,${VARNISH_MEMORY_SIZE}"    #設置緩存位置爲內存


2、啓動Varnish

[root@90sec /]# service varnish start

   Starting Varnish Cache:                                    [  OK  ]

[root@90sec /]# ss -anltp | grep varnish

LISTEN     0      128                      :::80                      :::*      users:(("varnishd",2500,8))

LISTEN     0      128                       *:80                       *:*      users:(("varnishd",2500,7))

LISTEN     0      10                127.0.0.1:6082                     *:*      users:(("varnishd",2499,6))

3、Varnishadm使用

[root@90sec /]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082

200        

-----------------------------

Varnish Cache CLI 1.0

-----------------------------

Linux,2.6.32-431.el6.x86_64,x86_64,-smalloc,-smalloc,-hcritbit

varnish-3.0.5 revision 1a89b1f

Type 'help' for command list.

Type 'quit' to close CLI session.

varnish> help

200        

help [command]

ping [timestamp]

auth response

quit         #退出

banner   

status        #查看狀態

start          #開啓

stop           #關閉

vcl.load <configname> <filename>       #加載一個VCL文件

vcl.inline <configname> <quoted_VCLstring>       

vcl.use <configname>                    #使用VCL文件

vcl.discard <configname>            

vcl.list                                #查看VCL文件列表

vcl.show <configname>                  #查看VCL文件配置

param.show [-l] [<param>]

param.set <param> <value>

panic.show

panic.clear

storage.list

backend.list

backend.set_health matcher state

ban.url <regexp>

ban <field> <operator> <arg> [&& <field> <oper> <arg>]...

ban.list

4、修改主配置文件

[root@90sec ~]# vim /etc/varnish/default.vcl 

import directors;

probe backend_healthcheck {

    .url = "/health.html";

    .window = 8;

    .threshold = 2;

    .interval = 3s;

}


backend web1 {

    .host = "172.16.36.130";

    .port = "80";

    .probe = backend_healthcheck;

}

backend web2 {

    .host = "172.16.36.131";

    .port = "80";

    .probe = backend_healthcheck;

}

backend APP1 {

    .host = "172.16.36.130";

    .port = "8080";

    .probe = backend_healthcheck;

}

backend app2 {

    .host = "172.16.36.131";

    .port = "8080";

    .probe = backend_healthcheck;

}

sub vcl_init {

    new web_cluster = directors.random();

    web_cluster.add_backend(web1,1.0);

    web_cluster.add_backend(web2,1.0);

    new img_cluster = directors.random();

    app_cluster.add_backend(app1,1.0);

    app_cluster.add_backend(app2,1.0);

}

acl purgers {

        "127.0.0.1";

        "172.16.0.0"/16:

}

sub vcl_recv {

    if (req.method == "GET" && req.http.cookie) {

      return(hash);

    }

    if (req.url ~ "test.html") {

        return(pass);

    }

    if (req.method == "PURGE") {

        if (!client.ip ~ purgers) {

            return(synth(405,"Method not allowed"));

       }

        return(hash);

    }

    if (req.http.X-Forward-For) {

        set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip;

    } else {

        set req.http.X-Forward-For = client.ip;

    }

    if (req.http.host ~ "(?i)^(www.)?lnmmp.com$") {

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

            set req.backend_hint = web_cluster.backend();

    } elsif (req.http.host ~ "(?i)^p_w_picpaths.lnmmp.com$") {

            set req.backend_hint = img_cluster.backend();

    }

    return(hash);

}

sub vcl_hit {

    if (req.method == "PURGE") {

        purge;

        return(synth(200,"Purged"));

    }

}

sub vcl_miss {

    if (req.method == "PURGE") {

        purge;

        return(synth(404,"Not in cache"));

    }

}

sub vcl_pass {

    if (req.method == "PURGE") {

        return(synth(502,"PURGE on a passed object"));

    }

}

sub vcl_backend_response {

        if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") {

                set beresp.ttl = 7200s;

        }

        if (bereq.url ~ "\.(html|css|js)$") {

                set beresp.ttl = 1200s;

        }

    if (beresp.http.Set-Cookie) {

        return(deliver);

    }

}

sub vcl_deliver {

    if (obj.hits > 0) {

        set resp.http.X-Cache = "HIT from " + server.ip;

    } else {

        set resp.http.X-Cache = "MISS";

    }

}


後面繼續更新

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章