varnish.md

varnish

webcache
命中率:hit/(hit+miss)
命中次數/(命中次數+未命中)css

文檔命中率:從文檔個數進行衡量:字節命中率:從內容大小進行衡量;html

緩存處理步驟
接收請求-->解析請求(提取請求的URL及各類首部)->查詢緩存-->新鮮度檢測-->建立響應報文-->發送響應-->記錄日誌web

varnish如何存儲緩存對象:
file:單個文件;不支持持久機制:
malloc:內存;
persistent:基於文件的持久存儲;算法

配置varnish的三種應用:
一、varnishd應用程序的命令行參數;
監聽的socket,使用的存儲類型等等;額外的配置參數;
-p param=value
-r param,param,..:設定只讀參數列表;
/etc/varnish/varnish.paramsshell

二、-p選項指明的參數:
運行時參數:
也可在程序運行中,經過其CLI進行配置:vim

三、vc1:配置緩存系統的緩存機制;
經過vc1配置文件進行配置:
先編譯,後應用:
依賴於c編譯器;後端

安裝 varnish

yum install varnish -y
cat /usr/lib/systemd/system/varnish.service
yum info jemalloc
man varnished

vim varnish.params
VARNISH_STORAGE="malloc,256M"

vim default.vcl
.host="192.168.100.102"  webservers
.port="80"
systemctl restart varnish

ss -tnl 6081 6082

varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
help
ping
status
vcl.list
vcl.load testname default.vcl
vcl.list
vcl.use testname
vcl.list
vcl.discard testname
vcl.list
vcl.show boot

param.show
param.set thread_pools 4
param.show -l 
param.show thread_pools

panic.show
storage.list
backend.list
ban.list
quit

可以使用telnet遠程登錄
#log
varnishlog  
vcrnishnsca
#top
varnishtop
#statistics
varnishstat

示例一:測試訪問緩存服務器,緩存是否命中

varnish server緩存

cat /etc/varnish/default.vcl
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "192.168.100.102";
    .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.method == "PRI"){
    /* we do not support SPDY or HTTP/2.0*/
    return(synth(405)); 
}
if(req.method != "GET" &&
    req.method != "HEAD" && 
    req.method != "PUT" &&
    req.method != "POST" &&
    req.method != "TRACE" &&
    req.method != "OPTIONS" &&
    req.method != "DELETE"){
    /*=Non-RFC2616 or CONNECT which is weird.*/
    return(pipe);
}
if(req.method !="GET" && req.method != "HEAD"){
    /* We only deal with GET and HEAD by default */
    return(pass); 
}
if(req.http.Authorization || req.http.Cookie){
    /* Not cacheable by default */
    return(pass);
}
	return(hash);
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
    if (obj.hits>0) {
	set resp.http.X-Cache="HIT";
	} else {
	set resp.http.X-Cache="MISS";	
}
}

web1,web2bash

for i in {1..10}; do echo "this is web1 page $i" > /var/www/html/test$i.html; done
systemctl restart httpd
systemctl enable httpd
for i in {1..10}; do echo "this is web2 page $i" > /var/www/html/test$i.html; done
systemctl restart httpd
systemctl enable httpd

客戶端測試服務器

for i in {1..2};do curl -I http://192.168.100.101:6081/test1.html;done|grep X-Cache

示例二:

強制對某資源的請求,不檢查緩存

/admin
/login

#訪問以 http://localhost/[login或/admin](不區分大小寫)不檢查緩存
sub vcl_recv{
if (req.url ~ "(?i)^/login" || req.url ~ "(?i)^/admin" ){
    return(pass);
}
}

webs

echo admin web2s > /var/www/html/admin/index.html

客戶端測試

curl http://192.168.100.101:6081/admin/index.html -I|grep MISS

示例三

對特定類型的資源取消其私有的COOKICE標識,並強行設定其能夠varnish緩存的時長

#訪問JPG 3600秒內都命中 HIT 
sub vcl_backend_response {
   if (beresp.http.cache-control !~ "s-maxage") {
  	 if (bereq.url ~ "(?i)\.txt$") {
        set beresp.ttl = 3600s;
        unset beresp.http.Set-Cookie;
   	 }
	if (bereq.url ~ "(?i)\.jpg$") {
        set beresp.ttl = 600s;
        unset beresp.http.Set-Cookie;
   	 }
	}
}

客戶端測試

curl http://192.168.100.101:6081/1.txt -I

示例四:

能夠作動靜分離

官方配置示例https://varnish-cache.org/trac/wiki/VCLExamples

backend server的定義

.host:BE主機的IP:
.port:BE主機監聽的PORT:

.probe:對BE作健康狀態檢測
.max_connections:併發鏈接最大數量

後端主機的健康狀態檢測方式

.url:斷定BE健康與否要請求的url;
.expected_response:指望響應狀態碼;默認爲200;

backend web1 {
    .host = "192.168.200.101";
    .port = "80";
	.probe = {
        .url = "/test1.html";
	}
}
backend web2 {
    .host = "192.168.200.102";
    .port = "80";
	.probe = {
        .url = "/test1.html";
	}
}

sub vcl_recv {  #此處只能出現一次,此有此項,將下面內容附加到後面便可
    if (req.url ~ "(?i)\.(jpg|pn|gif|txt)$") {
        set req.backend_hint = web1;
    } else {
        set req.backend_hint = web2;
    }
}

客戶端測試

curl http://192.168.100.101:6081/1.txt -I
curl http://192.168.100.101:6081/test1.html -I

示例五

負載均衡算法
fallback,random,round_robin,hash

#輪詢是對單個資源作round_robin
import directors;
sub vcl_init {
    new mycluster = directors.round_robin();
    mycluster.add_backend(web1);
    mycluster.add_backend(web2);
}

sub vcl_recv {
	set req.backend_hint = mycluster.backend();
}

客戶端測試

curl http://192.168.100.101:6081/test1.html
curl http://192.168.100.101:6081/test2.html

若要更改varnish端口

vim /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80

systemctl restart varnish

conf

#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend web1{
    .host = "192.168.100.101";
    .port = "80";
    .probe = {
	.url = "/test1.html";
	}
}
backend web2 {
    .host = "192.168.100.102";
    .port = "80";
    .probe = {
	.url = "/test1.html";
	}
}
import directors;
sub vcl_init {
    new mycluster = directors.round_robin();
    mycluster.add_backend(web1);
    mycluster.add_backend(web2);
}
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.
    set req.backend_hint = mycluster.backend();
    # if (req.url ~ "(?i)\.(jpg|png|gif|txt)$") {
    # set req.backend_hint = web1;
    #} else {
    # set req.backend_hint = web2;
    #}
    if (req.url ~ "(?i)^/login" || req.url ~ "(?i)^/admin" ){
    return(pass);
}
    if(req.method == "PRI"){
    /* we do not support SPDY or HTTP/2.0*/
    return(synth(405)); 
}
if(req.method != "GET" &&
    req.method != "HEAD" && 
    req.method != "PUT" &&
    req.method != "POST" &&
    req.method != "TRACE" &&
    req.method != "OPTIONS" &&
    req.method != "DELETE"){
    /*=Non-RFC2616 or CONNECT which is weird.*/
    return(pipe);
}
if(req.method !="GET" && req.method != "HEAD"){
    /* We only deal with GET and HEAD by default */
    return(pass); 
}
if(req.http.Authorization || req.http.Cookie){
    /* Not cacheable by default */
    return(pass);
}
	return(hash);
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
   if (beresp.http.cache-control !~ "s-maxage") {
    if (bereq.url ~ "(?i)\.txt$") {
        set beresp.ttl = 3600s;
        unset beresp.http.Set-Cookie;
    }
    if (bereq.url ~ "(?i)\.css$") {
        set beresp.ttl = 600s;
        unset beresp.http.Set-Cookie;
    }
}
}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
    if (obj.hits>0) {
	set resp.http.X-Cache="HIT";
	} else {
	set resp.http.X-Cache="MISS";	
}
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息