雖然使用nginx和http的時候都可以基於cache模塊使用緩存功能,但當用戶的併發上升到一定等級時,web服務自帶的緩存功能是遠遠不夠的,這時我們就需要一臺專門管理緩存的服務器。varnish是一款開源的緩存服務軟件,相對於squid更加的輕量級。
varnish的程序環境:
/etc/varnish/varnish.params: 配置varnish服務進程的工作特性,例如監聽
的地址和端口,緩存機制;
/etc/varnish/default.vcl:配置各Child/Cache線程的緩存策略;
主程序:
/usr/sbin/varnishd
CLI interface:
/usr/bin/varnishadm
VCL配置文件重載程序:
/usr/sbin/varnish_reload_vcl
varnish的緩存存儲機制( Storage Types):
-s [name=]type[,options]
· malloc[,size]
內存存儲,[,size]用於定義空間大小;重啓後所有緩存項失效;
· file[,path[,size[,granularity]]]
磁盤文件存儲,黑盒;重啓後所有緩存項失效;
· persistent,path,size
文件存儲,黑盒;重啓後所有緩存項有效;實驗;
通過yum安裝完varnish後,首先我們來看看varnish.params配置文件
# Varnish environment configuration description. This was derived from
# the old style sysconfig/defaults settings
# Set this to 1 to make systemd reload try to switch VCL without restart.
RELOAD_VCL=1
# Main configuration file. You probably want to change it.
VARNISH_VCL_CONF=/etc/varnish/default.vcl #指定默認的vcl文件
# Default address and port to bind to. Blank address means all IPv4
# and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
# quad, or an IPv6 address in brackets.
# VARNISH_LISTEN_ADDRESS=192.168.1.5
VARNISH_LISTEN_PORT=80 #指定監聽的端口
# Admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 #管理地址
VARNISH_ADMIN_LISTEN_PORT=6082
# Shared secret file for admin interface
VARNISH_SECRET_FILE=/etc/varnish/secret #口令文件
# Backend storage specification, see Storage Types in the varnishd(5)
# man page for details.
VARNISH_STORAGE="file,/var/varnish/cache,256M" #這個是我們主要改的,存儲的位置,方法形式
# User and group for the varnishd worker processes
VARNISH_USER=varnish
VARNISH_GROUP=varnish
# Other options, see the man page varnishd(1)
#DAEMON_OPTS="-p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300"
varnish程序的選項:
程序選項:/etc/varnish/varnish.params文件
-a address[:port][,address[:port][...],默認爲6081端口;
-T address[:port],默認爲6082端口;
-s [name=]type[,options],定義緩存存儲機制;
-u user
-g group
-f config:VCL配置文件;
-F:運行於前臺;
然後我們使用varnishadm來登陸接口varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
登陸進來使用help查看可用的命令
配置文件相關:
vcl.list
vcl.load:裝載,加載並編譯;
vcl.use:激活;
vcl.discard:刪除;
vcl.show [-v] <configname>:查看指定的配置文件的詳細信息;
運行時參數:
param.show -l:顯示列表;
param.show <PARAM>
param.set <PARAM> <VALUE>
緩存存儲:
storage.list
後端服務器:
backend.list
下面我們來介紹下VCL配置文件,裏面的功能是通過一種語言實現的。VCL有多個狀態引擎,狀態之間存在相關性,但狀態引擎彼此間互相隔離;每個狀態引擎可使用return(x)指明關聯至哪個下一級引擎;每個狀態引擎對應於vcl文件中的一個配置段,即爲subroutine。
vcl_recv的默認配置:
sub vcl_recv {
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);
}
}
裏面還有許許多多的變量
變量類型:
內建變量:
req.*:request,表示由客戶端發來的請求報文相關;
req.http.*
req.http.User-Agent, req.http.Referer, ...
bereq.*:由varnish發往BE主機的httpd請求相關;
bereq.http.*
beresp.*:由BE主機響應給varnish的響應報文相關;
beresp.http.*
resp.*:由varnish響應給client相關;
obj.*:存儲在緩存空間中的緩存對象的屬性;只讀;
常用變量:
bereq.*, req.*:
bereq.http.HEADERS
bereq.request:請求方法;
bereq.url:請求的url;
bereq.proto:請求的協議版本;
bereq.backend:指明要調用的後端主機;
req.http.Cookie:客戶端的請求報文中Cookie首部的值;
req.http.User-Agent ~ "chrome"
beresp.*, resp.*:
beresp.http.HEADERS
beresp.status:響應的狀態碼;
reresp.proto:協議版本;
beresp.backend.name:BE主機的主機名;
beresp.ttl:BE主機響應的內容的餘下的可緩存時長;
obj.*
obj.hits:此對象從緩存中命中的次數;
obj.ttl:對象的ttl值
server.*
server.ip
server.hostname
client.*
client.ip
用戶自定義:
set
unset
默認的VCL文件
vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "192.168.31.201";
.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.
}
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 acc