2015-12-15 69 views
1

我一直在關注如何設置haproxy和varnish。 (Link)但是,本教程是在幾年前編寫的。我一直在試圖改變V3清漆的配置,並在這部分所遇到的一個問題:升級清漆配置。 vcl_hit中的obj.ttl和beresp.ttl不起作用

sub vcl_hit { 
     # Purge 
     if (req.request == "PURGE") { 
       set obj.ttl = 0s; 
       error 200 "Purged."; 
     } 

     return (deliver); 
} 

set obj.ttl = 0s;是過時的。啓動服務時,它提供此錯誤:

Stopping Varnish Cache:         [FAILED] 
Starting Varnish Cache: Error: 

Variable 'obj.ttl' is read only. 
At: ('input' Line 46 Pos 21) 

有人說beresp.ttl應改爲使用,但它不是在方法vcl_hit支持:

Message from VCC-compiler: 
'beresp.ttl': cannot be set in method 'vcl_hit'. 
At: ('input' Line 95 Pos 21) 
       set beresp.ttl = 0s; 

--------------------##########------- 

Running VCC-compiler failed, exited with 2 
VCL compilation failed 

任何人都可以提出它的解決方案?

下面是我在漆配置到目前爲止改變:

vcl 4.0; 
backend apache2_static { 
     .host = "127.0.0.1"; 
     .port = "3001"; 
     .connect_timeout = 3s; 
     .first_byte_timeout = 10s; 
     .between_bytes_timeout = 5s; 
     .probe = { 
       .url = "/haproxycheck"; 
       .expected_response = 200; 
       .timeout = 1s; 
       .interval = 3s; 
       .window = 2; 
       .threshold = 2; 
       .initial = 2; 
     } 
} 

acl purge { 
     "localhost"; 
} 

sub vcl_recv { 
### Default options 

     # Health Checking 
     if (req.url == "/varnishcheck") { 
       return (synth(751, "health check OK!")); 
     } 

     # Set default backend 
     set req.backend_hint = apache2_static; 

     # grace period (stale content delivery while revalidating) 
     # 
     # This is now handled in vcl_hit. 
     # 
     # set req.grace = 30s; 

     # Purge request 
     if (req.method == "PURGE") { 
       if (!client.ip ~ purge) { 
         return (synth(405, "Not allowed.")); 
       } 
       return (hash); 
     } 

     # Accept-Encoding header clean-up 
     if (req.http.Accept-Encoding) { 
       # use gzip when possible, otherwise use deflate 
       if (req.http.Accept-Encoding ~ "gzip") { 
         set req.http.Accept-Encoding = "gzip"; 
       } elsif (req.http.Accept-Encoding ~ "deflate") { 
         set req.http.Accept-Encoding = "deflate"; 
       } else { 
         # unknown algorithm, remove accept-encoding header 
         unset req.http.Accept-Encoding; 
       } 

       # Microsoft Internet Explorer 6 is well know to be buggy with compression and css/js 
       if (req.url ~ ".(css|js)" && req.http.User-Agent ~ "MSIE 6") { 
         unset req.http.Accept-Encoding; 
       } 
     } 

### Per host/application configuration 
     # apache2_static 
     # Stale content delivery 
     # Cookie ignored in these static pages 
     unset req.http.cookie; 

### Common options 
     # Static objects are first looked up in the cache 
     if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") { 
       return (hash); 
     } 

     # if we arrive here, we look for the object in the cache 
     return (hash); 
} 

sub vcl_hash { 
     hash_data(req.url); 
     if (req.http.host) { 
       hash_data(req.http.host); 
     } else { 
       hash_data(server.ip); 
     } 
     return (lookup); 
} 

sub vcl_hit { 
     # Purge 
     if (req.method == "PURGE") { 
       set obj.ttl = 0s; 
       return (synth(200, "Purged.")); 
     } 

     return (deliver); 
} 

sub vcl_miss { 
     # Purge 
     if (req.method == "PURGE") { 
       return (synth(404, "Not in cache.")); 
     } 

     return (fetch); 
} 

sub vcl_backend_response { 
     # Stale content delivery 
     set beresp.grace = 1d; 

     # Hide Server information 
     unset beresp.http.Server; 

     # Store compressed objects in memory 
     # They would be uncompressed on the fly by Varnish if the client doesn't support compression 
     if (beresp.http.content-type ~ "(text|application)") { 
       set beresp.do_gzip = true; 
     } 

     # remove any cookie on static or pseudo-static objects 
     unset beresp.http.set-cookie; 

     return (deliver); 
} 

sub vcl_deliver { 
     unset resp.http.via; 
     unset resp.http.x-varnish; 

     # could be useful to know if the object was in cache or not 
     if (obj.hits > 0) { 
       set resp.http.X-Cache = "HIT"; 
     } else { 
       set resp.http.X-Cache = "MISS"; 
     } 

     return (deliver); 
} 

sub vcl_backend_error { 
     # Health check 
     if (beresp.status == 751) { 
       set beresp.status = 200; 
       return (deliver); 
     } 
} 

sub vcl_synth { 
     # Health check 
     if (resp.status == 751) { 
       set resp.status = 200; 
       return (deliver); 
     } 
} 
+2

您可能想閱讀https://www.varnish-software.com/book/4.0/chapters/Cache_Invalidation.html#http-purge – Ronald

回答

3

「清理部分」只應vcl_recvvcl_hit & vcl_miss刪除。

你也必須改變它在vcl_recv

sub vcl_recv { 
### Default options 

    # Health Checking 
    if (req.url == "/varnishcheck") { 
      return (synth(751, "health check OK!")); 
    } 

    # Set default backend 
    set req.backend_hint = apache2_static; 

    # grace period (stale content delivery while revalidating) 
    # 
    # This is now handled in vcl_hit. 
    # 
    # set req.grace = 30s; 

    # Purge request 
    if (req.method == "PURGE") { 
      if (!client.ip ~ purge) { 
        return (synth(405, "Not allowed.")); 
      } 
      return (purge); 
    } 

    # Accept-Encoding header clean-up 
    if (req.http.Accept-Encoding) { 
      # use gzip when possible, otherwise use deflate 
      if (req.http.Accept-Encoding ~ "gzip") { 
        set req.http.Accept-Encoding = "gzip"; 
      } elsif (req.http.Accept-Encoding ~ "deflate") { 
        set req.http.Accept-Encoding = "deflate"; 
      } else { 
        # unknown algorithm, remove accept-encoding header 
        unset req.http.Accept-Encoding; 
      } 

      # Microsoft Internet Explorer 6 is well know to be buggy with compression and css/js 
      if (req.url ~ ".(css|js)" && req.http.User-Agent ~ "MSIE 6") { 
        unset req.http.Accept-Encoding; 
      } 
    } 

### Per host/application configuration 
    # apache2_static 
    # Stale content delivery 
    # Cookie ignored in these static pages 
    unset req.http.cookie; 

### Common options 
    # Static objects are first looked up in the cache 
    if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") { 
      return (hash); 
    } 

    # if we arrive here, we look for the object in the cache 
    return (hash); 
} 

vcl_hit的方法

sub vcl_hit { 
    return (deliver); 
} 

vcl_miss方法

sub vcl_miss { 
    return (fetch); 
} 
1

vcl_hit()和vcl_miss()是不幸的是,清漆4.我有總是PURGE代碼路徑上不再成功使用Varnish 3來報告緩存失效。我向Varnish Software的人詢問了爲什麼它被刪除,他們只是說這是不準確的,因爲緩存中的obj可能有不同的變體。我不同意他們的決定,因爲驗證您要清除的變體實際上已被清除仍然有用。

在提供的其他答案中,我只是刪除vcl_hit()和vcl_miss()並讓默認處理程序運行,因爲您沒有添加任何附加條件。