varnish基本配置(二)
生活随笔
收集整理的這篇文章主要介紹了
varnish基本配置(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?具體的實(shí)例,假設(shè)我們有一個(gè)網(wǎng)站www.example.com, 需要增加varnish,來提升性能。
http header頭信息中的 Cache-Control: s-maxage 參數(shù)可以設(shè)置頁面在varnish中緩存的時(shí)間
?nginx配置:
server?{????listen?80
????server_name?www.example.com;
????location?/?{
????????proxy_pass?http://127.0.0.1:800;?#反向代理到varnish
????????proxy_set_header?Host?$host;
????????proxy_set_header?X-Real-IP?$remote_addr;
????????proxy_set_header?X-Forwarded-For?$proxy_add_x_forwarded_for;
????}
}
server?{
?????listen?8080;
?????server_name?www.example.com;
?????root?/var/www/example;
}
?
?example.vcl配置:
?backend?default?{?????.host?=?"127.0.0.1";
?????.port?=?"8080";
?????.connect_timeout?=?5s;
?????.first_byte_timeout?=?5s;
?????.between_bytes_timeout?=?5s;
?}
?sub?vcl_recv?{
?????set?req.backend=default;
?????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;
????????}
?????}
?????if?(req.request?!=?"GET"?&&
???????req.request?!=?"HEAD"?&&
???????req.request?!=?"PUT"?&&
???????req.request?!=?"POST"?&&
???????req.request?!=?"TRACE"?&&
???????req.request?!=?"OPTIONS"?&&
???????req.request?!=?"DELETE")?{
?????????/*?Non-RFC2616?or?CONNECT?which?is?weird.?*/
?????????return?(pipe);
?????}
?????if?(req.request?!=?"GET"?&&?req.request?!=?"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?(lookup);
?}
sub?vcl_hit?{
?????????#只要頁面刷新(F5、CTRL+F5),頁面的varnish?cache均失效
????????if(req.http.Cache-Control~"no-cache"||req.http.Cache-Control~"max-age=0"||req.http.Pragma~"no-cache"){
????????????????set?obj.ttl=0s;
????????????????return?(restart);
????????}
?????return?(deliver);
?}
sub?vcl_deliver?{
????????set?resp.http.x-hits=obj.hits;
????????if(obj.hits>0){
????????????????set?resp.http.X-Cache="hit";
????????}
????????else{
????????????????set?resp.http.X-Cache="MISS";
????????}
?????return?(deliver);
?}
?sub?vcl_fetch?{
?????if?(!beresp.cacheable)?{
?????????return?(pass);
?????}
?????if?(beresp.http.Set-Cookie)?{
?????????return?(pass);
?????}
#if(beresp.http.Pragma~"no-cache"||beresp.http.Cache-Control~"no-cache"||beresp.http.Cache-Control~"private"){
#????????return?(pass);
#????}
?????if(req.request=="GET"&&req.http.host~"bbs.xiazh.dev.aifang.com$"){
?????????set?beresp.ttl=1200s;
?????}
??????if(req.request=="GET"?&&?req.url~"\.(png|swf|txt|jpg|css|js|html|htm)$"){
?????????set?beresp.ttl=3600s;
???????}
??????????return?(deliver);
?}
?
總結(jié)
以上是生活随笔為你收集整理的varnish基本配置(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。