Varnish how to store cached objects:
File: Single document; persistence mechanism not supported;
malloc: Cached in memory;
Persistent: file-based persistent storage (not recommended in this way)
VCL: Configure caching mechanism of cache system; "How caching works in Threads"
First, install HTTP on VS2 and VS3
Write to the file, one of the contents is on VS2 and the other is on VS3
[[email protected] ~]# yum install http[[email protected] ~]# for i in {1..10}; Do echo "web$i on VS2" >/var/www/html/test$i.html; Done[[email protected] ~]# ls/var/www/html/test10.html test2.html test4.html test6.html test8.htmltest1.html test3. HTML test5.html test7.html test9.html[[email protected] ~]# systemctl start Httpd.service
Second, install the varnish (installed on the CENTOS7 4.0.3 version)
[email protected] ~]# Yum install varnish
Third, the varnish main program configuration file
[[email protected] ~]# vim /etc/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.5varnish_listen_port=6081 #监听的服务端口为6081 # admin interface listen address and portvarnish_admin_ listen_address=127.0.0.1 #监听的管理地址为本机VARNISH_ADMIN_LISTEN_PORT =6082 #监听的管理端口为6082 # shared secret file for admin interfacevarnish_secret_file=/ etc/varnish/secret #密钥文件位置 # backend storage specification, see storage types in the varnishd (5) # man page for details. Varnish_storage= "file,/var/lib/varnish/varnish_storage.bin,1g" #缓存以文件的方式的存储位置和大小 # Varnish_storage= "malooc,256m" #以内存方式缓存, cache size is 256m# default ttl used when the backend does not specify onevarnish_ttl=120 # Contact backend server time-out timeout # user and group for the varnishd worker processesvarnish_ user=varnish #主进程所使User varnish_group=varnish #主进程所使用的组 # other options, see the MAN PAGE VARNISHD (1) #进程选项, maximum minimum and timeout for the thread pool daemon_opts= "-p thread_pool_ Min=5 -p thread_pool_max=500 -p thread_pool_timeout=300 "
Iv. varnish command-line management tools
[[email protected] ~]# varnishadm -s /etc/varnish/secret -t 127.0.0.1:6082help help [<command>] # Get help information ping [<timestamp>] #测试服务器是否正常auth <response> #quit #退出bannerstatus #显示服务器状态信息start #启动子进程stop #停止子进程vcl .load <configname> <filename> #载入哪个文件为配置文件vcl. Inline <configname> <quoted_vclstring>vcl.use <configname> #使用哪个vcl文件vcl. Discard <configname> #删除哪个vcl文件vcl .list #列出所有可用的vcl文件param .show [-l] [<param>] #显示运行时参数param .set <param> <value> panic.show #显示恐慌信息, Shows the reason that the process or child process was last hung out panic.clear #清除恐慌信息storage .list #显示缓存信息vcl .show [-v] < configname> #显示vcl文件的详细信息, Backend.list [<backend_ before the VCL compiles expression>] #显示后端服务器列表backend. set_health <backend_expression> <state> # Manual offline back-end server ban <field> <operator> <arg> [&& <field> <oper> <arg>]... #清理缓存中的缓存对象ban .list #显示定义的清理缓存规则
Access logs for varnish
[Email protected] ~]# varnishlog[[email protected] ~]# varnishtop
Varnish statistical information
[Email protected] ~]# Varnishstat
V. Description of the VCL configuration file
Http://book.varnish-software.com/4.0/chapters/VCL_Basics.html
Http://book.varnish-software.com/4.0/_images/simplified_fsm.svg
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466011145365271.jpg "title= "1466011145365271.jpg" alt= "Varnish.jpg"/>
VCL_RECV Receive request cacheable determine whether a Cacheable object Incache determine whether the result of a hash is present Vcl_hash cacheable object hash calculation Vcl_hit cache hit in Vcl_miss cache Miss Vcl_fetch get back-end content Vcl_deliver Build cache send Vcl_pipe Client request method is not a common method, directly to the backend server processing Vcl_pass do not check the cache directly from the back-end server fetch vcl_er Ror varnish directly returns an error response
Six, VCL configuration file
[[email protected] ~]# cp /etc/varnish/default.vcl /etc/varnish/test.vcl[[email Protected] ~]# vim /etc/varnish/test.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 vs2 { #定义后端主机vs2 .host = "172.16.24.102"; .port = "; .probe = { " #对后端主机的test1进行进行健康状态检测 .url = "/test1.html"; }}backend vs3 { .host = "172.16.24.104"; .port = "; .probe = { " .url = "/test1.html"; }} #import directors; #加载directors模块, use #sub vcl_init { during load Balancing polling #轮询方式的负载均衡 # new mycluster = Directors.round_robin (); # &nbSp; mycluster.add_backend (VS2); # mycluster.add_backend (VS3); #}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. #url中开头带有login或者admin的直接从后端主机取结果不缓存 if (req.url ~ "(? I ) ^/login " | | req.url ~ "(? i) ^/admin") { Return (pass); } #url以jpg, Png,gif end of the direct distribution to VS2, the others are sent to vs3 if (req.url ~ "(? i) \. ( Jpg|png|gif) ($ ") { set req.backend_hint = vs2; } else { set req.backend_hint = vs3; }# set req.backend_hint = mycluster.backend (); #负载均衡集群 # If the client request returns 405 for the PRI, if the requested non-Get,head,put,post,trace,options,delete are sent directly to the backend host for processing 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" &NBSP;&Amp;& 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. #如果缓存能命中就在返回值中插入HIT, miss inserted if missing if (obj.hits>0) { set resp.http.x-cache = "hit"; } else { set resp.http.x-cache = "MISS"; }}[[email protected] ~]# varnishadm -s /etc/varnish/secret -t 127.0.0.1:6082vcl.load test2 test.vcl200 VCL compiled.vcl.use test2200 VCL ' Test2 ' now active
Seven, testing
Upload a dog.jpg on VS2 and don't upload any pictures on VS3
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466017455468062.png "title= "1466017455468062.png" alt= "Blob.png"/>
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466017477103436.png "title= "1466017477103436.png" alt= "Blob.png"/>
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466017498592277.png "title= "1466017498592277.png" alt= "Blob.png"/>
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466017528888636.png "title= "1466017528888636.png" alt= "Blob.png"/>
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466019052650261.png "title= "1466019052650261.png" alt= "Blob.png"/>
650) this.width=650; "src=" Http://www.178linux.com/ueditor/php/upload/image/20160616/1466019125541130.png "title= "1466019125541130.png" alt= "Blob.png"/>
This article is from the "Wind" blog, please be sure to keep this source http://xsllqs.blog.51cto.com/2308669/1791225
Build httpd cache server with varnish