2012-04-06 111 views
1

我試圖使用清漆緩存提供gzip壓縮。但是當我使用下面提到的清漆配置(default.vcl)將content-encoding設置爲gzip時。瀏覽器無法下載我爲其設置內容編碼的內容。使用清漆緩存的gzip壓縮

光油配置文件:

backend default { 
     .host = "127.0.0.1"; 
     .port = "9000"; 
} 

backend socketIO { 
     .host = "127.0.0.1"; 
     .port = "8083"; 
} 

acl purge { 
     "127.0.0.1"; 
     "192.168.15.0"/24; 
} 

sub vcl_fetch { 
    /* If the request is for pictures, javascript, css, etc */ 
     if (req.url ~ "^/public/" || req.url ~ "\.js"){  
     unset req.http.cookie;  
     set beresp.http.Content-Encoding= "gzip"; 
     set beresp.ttl = 86400s; 
     set beresp.http.Cache-Control = "public, max-age=3600"; 
     /*set the expires time to response header*/ 
     set beresp.http.expires=beresp.ttl;  
       /* marker for vcl_deliver to reset Age: */ 
       set beresp.http.magicmarker = "1"; 
    } 

    if (!beresp.cacheable) { 
     return (pass); 
    } 
    return (deliver); 
} 
sub vcl_deliver { 
    if (resp.http.magicmarker) { 
     /* Remove the magic marker */ 
     unset resp.http.magicmarker; 

     /* By definition we have a fresh object */ 
     set resp.http.age = "0"; 
    } 

    if(obj.hits > 0) { 
     set resp.http.X-Varnish-Cache = "HIT"; 
    }else { 
     set resp.http.X-Varnish-Cache = "MISS"; 
    } 

    return (deliver); 
} 

sub vcl_recv { 
    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); 
     } 
    # Pass requests that are not GET or HEAD 
    if (req.request != "GET" && req.request != "HEAD") { 
     return(pass); 
    }   

    #pipe websocket connections directly to Node.js 
    if (req.http.Upgrade ~ "(?i)websocket") { 
     set req.backend = socketIO; 
     return (pipe); 
    } 

    # Properly handle different encoding types 
     if (req.http.Accept-Encoding) { 
     if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|js|css)$") { 
      # No point in compressing these 
      remove req.http.Accept-Encoding; 
     } elsif (req.http.Accept-Encoding ~ "gzip") { 
      set req.http.Accept-Encoding = "gzip"; 
     } elsif (req.http.Accept-Encoding ~ "deflate") { 
      set req.http.Accept-Encoding = "deflate"; 
     } else { 
      # unkown algorithm 
      remove req.http.Accept-Encoding; 
     } 
     } 

    # allow PURGE from localhost and 192.168.15... 
     if (req.request == "PURGE") { 
       if (!client.ip ~ purge) { 
         error 405 "Not allowed."; 
       } 
       return (lookup); 
     } 
    return (lookup); 
} 

sub vcl_hit { 
     if (req.request == "PURGE") { 
       purge_url(req.url); 
       error 200 "Purged."; 
     } 
} 

sub vcl_miss { 
     if (req.request == "PURGE") { 
       purge_url(req.url); 
       error 200 "Purged."; 
     } 
} 

sub vcl_pipe { 
    if (req.http.upgrade) { 
     set bereq.http.upgrade = req.http.upgrade; 
    } 
} 

響應頭:

Cache-Control:public, max-age=3600 
Connection:keep-alive 
Content-Encoding:gzip 
Content-Length:11520 
Content-Type:application/javascript 
Date:Fri, 06 Apr 2012 04:53:41 GMT 
ETag:"1330493670000--987570445" 
Last-Modified:Wed, 29 Feb 2012 05:34:30 GMT 
Server:Play! Framework;1.2.x-localbuild;dev 
Via:1.1 varnish 
X-Varnish:118464579 118464571 
X-Varnish-Cache:HIT 
age:0 
expires:86400.000 

上如何解決此問題以及如何使用清漆提供gzip壓縮任何建議。

回答

5

Varnish Cache 3.0自動處理Accept-Encoding的大部分內容,你不應該搞砸它。

基本上,如果你想讓Varnish在vcl_fetch中設置beresp.do_gzip這個對象,它會在壓縮之前將其存儲在緩存中。需要時會自動解壓縮。

1

內容長度似乎是錯誤的,請嘗試取消設置它。另外,爲什麼在vcl_fetch中使用beresp而不是obj?

... 
obj.http.Content-Encoding="gzip"; 
remove obj.http.Content-Length; 
... 
0

檢查並看看你是否與Apache,並啓用mod_deflate, 嘗試禁用它。