标题索引php
追朔缘由css
实验分解html
抓包分析java
追朔缘由【此文须要待续】nginx
当下是互联网时代也是CDN缓存时代,缓存能够提供互联网80%流量,所以缓存的构建和提升缓存的命中率是互联网行业必不可少的方式和手段,另外在http1.1协议盛行的时代,需很是了解http协议才能对apache、nginx、haproxy等服务器原理了解更为透彻,互联网CDN内容发布商缓存命中率才能大大提供,用户才能获得好的体验。
apache
实验分解后端
当客户端浏览器cache-control:max-age=0时,表示缓存服务器一般须要将请求转发给源服务器(具体可参考图解http协议书籍89页);
浏览器
当客户端浏览器cache-control:max-age>0时,表示客户端能够从缓存服务器端直接获取数据。
缓存
vcl 4.0; #------------------------------------------------------- #装载负载均衡模块 import directors; import std; #------------------------------------------------------- #在全局配置中配置acl,在recv函数中调用acl acl aclpurges { "192.168.40.2"/24; "127.0.0.1"/24; } #------------------------------------------------------- #定义健康性检测,方便backend后端调用 probe healthcheck { .url = "/index.html"; .window = 5; .threshold = 4; .interval = 10s; .timeout = 2s; } #-------------------------------------------------------- #定义backend后端服务器 backend phpserver01 { .host = "192.168.40.1"; .port = "80"; .probe = healthcheck; } backend phpserver02 { .host = "192.168.40.2"; .port = "80"; .probe = healthcheck; } backend picserver11 { .host = "192.168.40.11"; .port = "80"; .probe = healthcheck; } backend picserver12 { .host = "192.168.40.12"; .port = "80"; .probe = healthcheck; } backend javaserver21{ .host = "192.168.40.21"; .port = "80"; .probe = healthcheck; } backend javaserver22{ .host = "192.168.40.22"; .port = "80"; .probe = healthcheck; } #vcl初始化组信息 sub vcl_init{ #定义后端服务器组 #若需会话绑定时 #new php_group = directors.hash(); new php_group = directors.round_robin(); new pic_group = directors.round_robin(); new java_group = directors.round_robin(); #default_group php_group.add_backend(phpserver01); php_group.add_backend(phpserver02); #picserver_group pic_group.add_backend(picserver11); pic_group.add_backend(picserver12); #javaserver_group java_group.add_backend(javaserver21); java_group.add_backend(javaserver22); } #-------------------------------------------------------- sub vcl_recv { #+++++++对不一样资源进行分离调度 #若需会话绑定时,分组中已经组,只需根据cookie进行hash #set req.backend_hint = php_group.backend(req.http.cookie); if (req.url ~ "(?i)\.(jpg|jpeg|png|gif)$"){ set req.backend_hint = pic_group.backend(); } if (req.url ~ "(?i)\.(jsp)"){ set req.backend_hint = java_group.backend(); }else{ set req.backend_hint = php_group.backend(); } #+++++++对特定资源直接调用pass函数进行反向代理 if ( req.url ~ "(?i)^/(login|admin)"){ return(pass); } #+++++++对URL发生改变的请求,即URL重写的请求X-forwarded-For if (req.restarts == 0 ){ if (req.http.X-Forwarded-For) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } #+++++++对缓存资源进行修剪,默认基于URL进行缓存,修剪时需保持URL不变 if ( req.method == "PURGE" ){ if ( !client.ip ~ aclpurges) { #访问时http://172.18.27.24/123.jpg #修剪时curl -X PURGE http://172.18.27.24/123.jpg return(synth(405,"You are not allowed" + client.ip)); } return (purge); } #+++++++当对一类资源进行修改时能够经过varnishadmin命令行修剪 #如:ban req.rul ~ (?i).(jpg|pgn)批量进行修剪 if (req.method != "GET" && req.method != "HEAD") { return (pipe); } #+++++++定义缓存的内容 if (req.url ~ "\.(jpe?g|png|gif|pdf|gz|tgz|bz2|tbz|tar|zip|tiff|tif)$" || req.url ~ "/(p_w_picpath|(p_w_picpath_(?:[^/]|(?!view.*).+)))$") { return (hash); } if (req.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv|flv)$") { return (hash); } if (req.url ~ "\.(xls|vsd|doc|ppt|pps|vsd|doc|ppt|pps|xls|pdf|sxw|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") { return (hash); } if (req.url ~ "\.(css|js)$") { return (hash); } if (req.http.Authorization || req.http.Cookie ~ "(^|; )(__ac=|_ZopeId=)") { /* Not cacheable by default */ return (pipe); } } #-------------------------------------------------------- sub vcl_backend_response { #+++++++对特定资源强行进行缓存 if ( beresp.http.cache-control !~ "s-maxage" ) { if ( bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js)$" ) { unset beresp.http.Set-Cookie; set beresp.ttl = 3600s; } } #定义缓存策略 if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") { # Mark as "Hit-For-Pass" for the next 60 minutes - 24 hours if (bereq.url ~ "\.(jpe?g|png|gif|pdf|gz|tgz|bz2|tbz|tar|zip|tiff|tif)$" || bereq.url ~ "/(p_w_picpath|(p_w_picpath_(?:[^/]|(?!view.*).+)))$") { set beresp.ttl = std.duration(beresp.http.age+"s",0s) + 6h; } elseif (bereq.url ~ "\.(svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv|flv)$") { set beresp.ttl = std.duration(beresp.http.age+"s",0s) + 6h; } elseif (bereq.url ~ "\.(xls|vsd|doc|ppt|pps|vsd|doc|ppt|pps|xls|pdf|sxw|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") { set beresp.ttl = std.duration(beresp.http.age+"s",0s) + 6h; } elseif (bereq.url ~ "\.(css|js)$") { set beresp.ttl = std.duration(beresp.http.age+"s",0s) + 24h; } else { set beresp.ttl = std.duration(beresp.http.age+"s",0s) + 1h; } } } #--------------------------------------------------------- sub vcl_deliver { #构建http报头时添加命中或非命中字段 if (obj.hits>0) { set resp.http.X-cache = "Hit via" + server.ip; } else { set resp.http.X-cache = "Miss from" + server.ip; } } #---------------------------------------------------------- #varnish优化, #在运行时进行调整相关参数,经过命令进行调整, #thread_pools:最大池子数 param.set thread_pools 4 #thread_pool_max:池子最大线程数 param.set thread_max 8000 #thread_pool_min:池子最小线程数 param.set thread_max 500 #永久性优化时,经过编写配置文件进行调整参数 #DAEMON_OPTS="-p thread_pool_min=500 -p thread_pool_max=8000 -p thread_pools=4"