2012-04-06 31 views
3

我有一个竞争对手窃取我们的内容,并且由于一些侦查,我在我们的日志中发现了他们的IP地址。有谁知道我可以如何使用Varnish从第二个陈旧的缓存中为我们的网站提供服务?我希望他们获得该网站,但只是旧内容。如何清除第二个缓存到特定的IP

我甚至不确定这是否可以工作,但这里是我想出了使用Varnish 3尊敬页面的原因。我会在这里做这个诀窍吗?

backend longcache { 
    .host = "127.0.0.1"; 
    .port = "8080"; 
    .connect_timeout = 6s; 
    .first_byte_timeout = 3s; 
    .between_bytes_timeout = 3s; 
} 

acl longcachegroup { 
    "255.255.255.255";  // the bad ip 
} 

if (client.ip ~ longcachegroup) { 
    set req.backend = longcache; 

    sub vcl_fetch { 
if (req.url ~ "^/*") { 
    unset beresp.http.cookie; 
} 

# A TTL of Long ass time minutes 
    set beresp.ttl = 999999999999s; 
} 
} 

这是我目前的default.vcl

backend default { 
    .host = "127.0.0.1"; 
    .port = "8080"; 
    .connect_timeout = 600s; 
    .first_byte_timeout = 600s; 
    .between_bytes_timeout = 600s; 
} 

sub vcl_recv { 
    # Add a unique header containing the client address 
    remove req.http.X-Forwarded-For; 
    set req.http.X-Forwarded-For = client.ip; 
} 


sub vcl_recv { 

# A configuration file specific for Drupal 7 that also seems to work on Drupal 6 

# Either the admin pages or the login 
if (req.url ~ "/admin/?") { 
     # Don't cache, pass to backend 
     return (pass); 
} 

#for anonymous search and poll votes 
if (req.request ~ "POST") { 
    return (pass); 
} 




# Remove the "has_js" cookie 
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(;)?", ""); 

# Remove the "Drupal.toolbar.collapsed" cookie 
set req.http.Cookie = regsuball(req.http.Cookie, "Drupal.toolbar.collapsed=[^;]+(;)?", ""); 


# Remove any Google Analytics based cookies 
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(;)?", ""); 

# Remove the Quant Capital cookies (added by some plugin, all __qca) 
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(;)?", ""); 

# Are there cookies left with only spaces or that are empty? 
if (req.http.cookie ~ "^ *$") { 
     unset req.http.cookie; 
} 

# Static content unique to the theme can be cached (so no user uploaded images) 
if (req.url ~ "^/themes/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") { 
     unset req.http.cookie; 
} 
# Cache images 
if (req.url ~ "^/files/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") { 
     unset req.http.cookie; 
} 





# Normalize Accept-Encoding header (straight from the manual: https://www.varnish-cache.org/docs/3.0/tutorial/vary.html) 
if (req.http.Accept-Encoding) { 
     if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { 
       # 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; 
     } 
} 

# Don't cache the install, update or cron files in Drupal 
if (req.url ~ "install\.php|update\.php|cron\.php|current\.html|^/user|fupload/flash|stream\.html") { 
    return (pass); 
} 

# Uncomment this to trigger the vcl_error() subroutine, which will HTML output you some variables (HTTP 700 = pretty debug) 
#error 700; 

# Anything else left? 
if (!req.http.cookie) { 
     unset req.http.cookie; 
} 


if (req.http.Authorization || req.http.Cookie) { 
    # Not cacheable by default 
    return (pass); 
} 

# Try a cache-lookup 
return (lookup); 

} 


sub vcl_fetch { 

# For static content related to the theme, strip all backend cookies 
if (req.url ~ "^/themes/" && req.url ~ "\.(css|js|png|gif|jp(e?)g)") { 
    unset beresp.http.cookie; 
} 

# A TTL of 15 minutes 
    set beresp.ttl = 900s; 
} 

sub vcl_error { 

    set obj.http.Content-Type = "text/html; charset=utf-8"; 
    set obj.http.Retry-After = "5"; 
    synthetic {" 
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
    <head> 
    <title>"} + obj.status + " " + obj.response + {"</title> 
    </head> 
    <body> 
    <h1>Error "} + obj.status + " " + obj.response + {"</h1> 
    <p>"} + obj.response + {"</p> 
    <h3>Guru Meditation:</h3> 
    <p>XID: "} + req.xid + {"</p> 
    <hr> 
    <p>Varnish cache server</p> 
    </body> 
</html> 
"}; 

} 

回答

1

你会被重写你的TTL缓存中的每个人都一样,因为它会得到相同的哈希值作为你的靶向一个。

什么,你需要做的,以及是也添加类似:

sub vcl_hash { 

    ### these 2 entries are the default ones used for vcl. Below we add our own. 
    set req.hash += req.url; 
    set req.hash += req.http.host; 

    if (client.ip ~ longcachegroup) { 
     set req.hash += "longcachegroup"; 
    } 
}