varnish缓存服务器构建疑问
生活随笔
收集整理的這篇文章主要介紹了
varnish缓存服务器构建疑问
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
標題索引
-
追朔原因
-
實驗分解
-
抓包分析
追朔原因【此文需要待續】
????當下是互聯網時代也是CDN緩存時代,緩存可以提供互聯網80%流量,因此緩存的構建和提高緩存的命中率是互聯網行業必不可少的方式和手段,另外在http1.1協議盛行的時代,需非常了解http協議才能對apache、nginx、haproxy等服務器原理了解更為透徹,互聯網CDN內容發布商緩存命中率才能大大提供,用戶才能得到好的體驗。
實驗分解
????當客戶端瀏覽器cache-control:max-age=0時,表示緩存服務器通常需要將請求轉發給源服務器(具體可參考圖解http協議書籍89頁);
????當客戶端瀏覽器cache-control:max-age>0時,表示客戶端可以從緩存服務器端直接獲取數據。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 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?~?"/(image|(image_(?:[^/]|(?!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?~?"/(image|(image_(?:[^/]|(?!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" |
本文轉自 薛偉博 51CTO博客,原文鏈接:http://blog.51cto.com/weiboxue/1978570,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的varnish缓存服务器构建疑问的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模式与方法论
- 下一篇: MySQL5.5编译方式安装实战