清漆返回不正确的后端

时间:2015-10-19 01:33:30

标签: wordpress caching varnish

我正在尝试为我的Varnish配置设置多个后端,最近在我的服务器上添加了一些新域名/网站。

以下完整的VCL ......

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

backend scrum {
    .host = "127.0.0.2";
    .port = "8080";     
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;  
}

backend striker {
    .host = "127.0.0.3";
    .port = "8080";     
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;  
}

backend throttle {
    .host = "127.0.0.4";
    .port = "8080";     
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;  
}

backend ussports {
    .host = "127.0.0.5";
    .port = "8080";     
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;  
}

acl purge {
    "localhost";
    "127.0.0.1";
    "127.0.0.2";
    "127.0.0.3";
    "127.0.0.4";
    "127.0.0.5";
    "127.0.0.6";
    "127.0.0.7";
    "127.0.0.8";                            
    "54.253.117.205";
}

sub vcl_recv {
    # Backends
    set req.backend = default;
    if (req.http.host ~ "(?i)^(www.)?scrum.com$") {
        set req.http.host = "www.scrum.com";
        set req.backend = scrum;
    } 
    if (req.http.host ~ "(?i)^(www.)?striker.com$") {
        set req.http.host = "www.striker.com";  
        set req.backend = striker;  
    } 
    if (req.http.host ~ "(?i)^(www.)?throttle.com$") {
        set req.http.host = "www.throttle.com"; 
        set req.backend = throttle;
    } 
    if (req.http.host ~ "(?i)^(www.)?ussports.com$") {
        set req.http.host = "www.ussports.com"; 
        set req.backend = ussports;             
    }

    # Don't cache these for now
    if (req.http.host ~ "(www\.)?(digital.com|hanger.com|wicket.com)") {    
        return (pass);
    }       

    # Don't cache admin
    if (req.http.host == "sendy.tackle.com" || req.http.host == "admin.tackle.com" || req.http.host == "admin.hanger.com" || req.http.host == "admin.wicket.com" || req.http.host == "admin.striker.com" || req.http.host == "admin.scrum.com" || req.http.host == "admin.throttle.com" || req.http.host == "admin.ussports.com") {
        return (pass);
    }   

    if (req.request == "PURGE") {
        if (!client.ip ~ purge) {
            error 405 "Not allowed.";
        }
        ban("req.http.host == " + req.http.host);
    }

    # Normalize content-encoding
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(\?.*|)$") {
            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 {
            remove req.http.Accept-Encoding;
        }
    }

    if (req.restarts == 0) {
        if (req.http.CF-Connecting-IP) {
            set req.http.X-Forwarded-For = req.http.CF-Connecting-IP;
        } else {
            set req.http.X-Forwarded-For = client.ip;
        }
    }

    # Device detection
    set req.http.X-Device = "desktop";
    if ( req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" ) {
        set req.http.X-Device = "smart";
    }           

    # don't cache authenticated sessions
    if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID)") {
        return(pass);
    }

    # Remove cookies and query string for real static files
    if (req.url ~ "^/[^?]+\.(gif|jpg|jpeg|swf|css|js|txt|flv|mp3|mp4|pdf|ico|png|gz|zip|lzma|bz2|tgz|tbz)(\?.*|)$") {
       unset req.http.cookie;
       set req.url = regsub(req.url, "\?.*$", "");
    }

    # Don't cache backend
    if (req.url ~ "wp-(login|admin|comments-post.php|cron.php)" || req.url ~ "preview=true") {
        return (pass);
    }

    # Don't cache backend
    if (req.url ~ "(discussion|server-status|pick-your-test-team|pick-your-team|apc.php|xmlrpc.php|_showprocesslist.php)") {
        return (pass);
    }           

    if (req.request == "POST")
    {
        return(pass);
    }   

    return (lookup);
}

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

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

sub vcl_fetch {
    # Don't cache these for now
    if (req.http.host ~ "(www\.)?(digital.com|hanger.com|wicket.com)") {    
        return (hit_for_pass);
    }   

    # Don't cache admin
    if (req.http.host == "sendy.tackle.com" || req.http.host == "admin.tackle.com" || req.http.host == "admin.hanger.com" || req.http.host == "admin.wicket.com" || req.http.host == "admin.striker.com" || req.http.host == "admin.scrum.com" || req.http.host == "admin.throttle.com" || req.http.host == "admin.ussports.com") {
        return (hit_for_pass);
    }       

    # Don't store backend
    if (req.url ~ "wp-(login|admin|comments-post.php|cron.php)" || req.url ~ "preview=true") {
        return (hit_for_pass);
    }

    # If WordPress cookies found then page is not cacheable
    if (req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
        return (hit_for_pass);
    }

    # Don't cache backend
    if (req.url ~ "(discussion|server-status|pick-your-test-team|pick-your-team|apc.php|xmlrpc.php|_showprocesslist.php)") {
        return (hit_for_pass);
    }       

    if ( (!(req.url ~ "(wp-(login|admin|comments-post.php|server-status|discussion|cron.php)|login)")) || (req.request == "GET") ) {
        unset beresp.http.set-cookie;
        set beresp.ttl = 4h;
    }
    if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|txt|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
        set beresp.ttl = 30d;
    } 
    #else {
    # set beresp.do_esi = true;
    #}
}


sub vcl_deliver {
# add debugging headers, so we can see what's cached
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
# remove some headers added by varnish
    unset resp.http.Via;
    unset resp.http.X-Varnish;
}
#

sub vcl_hash {
    hash_data( req.url );

    # And then add the device to the hash (if its a mobile device)
    if (req.http.X-Device ~ "^smart") {
        hash_data( req.http.X-Device ); 
    }   

# altering hash so subdomains are ignored.
# don't do this if you actually run different sites on different subdomains
#   if ( req.http.host ) {
#       hash_data( regsub( req.http.host, "^([^\.]+\.)+([a-z]+)$", "\1\2" ) );
#   } else {
#       hash_data( server.ip );
#   }
# ensure separate cache for mobile clients (WPTouch workaround)
    if( req.http.User-Agent ~ "(iPod|iPhone|incognito|webmate|dream|CUPCAKE|WebOS|blackberry9\d\d\d)" ){
        hash_data("touch");
    }
    return (hash);
}

现在,我的默认网站www.tackle.com工作正常并且有一段时间了。正如我所说,我已经在服务器中添加了一些新站点,并希望从Varnish中提供它们。

我发现有点随机,如果我访问www.scrum / throttle / striker.com,我最终会收到www.tackle.com主页。所以,我显然有些不对劲。

知道我缺少什么吗?

0 个答案:

没有答案