varnish基本配置(二)

 具体的实例,假设我们有一个网站www.example.com, 需要增加varnish,来提升性能。

http header头信息中的 Cache-Control: s-maxage 参数可以设置页面在varnish中缓存的时间

 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);
 }

 

转载于:https://www.cnblogs.com/xiazh/archive/2011/07/12/2098850.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值