
backend img1 {
	.host = "172.16.22.11";
	.port = "82";
	.connect_timeout = 5s;
	.first_byte_timeout = 5s;
	.between_bytes_timeout = 2s;
        .probe = {
                .url = "/images/testfiledonotdelete.html";
                .interval = 5s;
                .timeout = 1 s;
                .window = 5;
                .threshold = 3;
       }

}

backend img2 {
	.host = "172.16.22.13";
	.port = "82";
	.connect_timeout = 5s;
	.first_byte_timeout = 5s;
	.between_bytes_timeout = 2s;
        .probe = {
                .url = "/images/testfiledonotdelete.html";
                .interval = 5s;
                .timeout = 1 s;
                .window = 5;
                .threshold = 3;
       }
}

backend www1 {
	.host = "172.16.22.2";
	.port = "80";
	.connect_timeout = 5s;
	.first_byte_timeout = 5s;
	.between_bytes_timeout = 2s;
        .probe = {
                .url = "/robots.txt";
                .interval = 5s;
                .timeout = 1 s;
                .window = 5;
                .threshold = 3;
       }

}

backend www2 {
	.host = "172.16.22.3";
	.port = "80";
	.connect_timeout = 5s;
	.first_byte_timeout = 5s;
	.between_bytes_timeout = 2s;
        .probe = {
                .url = "/robots.txt";
                .interval = 5s;
                .timeout = 1 s;
                .window = 5;
                .threshold = 3;
       }
}


director img_director round-robin {
	{ .backend = img1; }
	{ .backend = img2; }
}

director www_director round-robin {
	{ .backend = www1; }
	{ .backend = www2; }
}


sub vcl_recv {

	unset req.http.Cookie;

	if (req.http.Accept-Encoding) {
	    if (req.url ~ "\.(jpg|png|gif)$") {
		# 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;
	    }
	}

	if ( req.url ~ "/templates/common/" ) {
		set req.backend = www_director;
	} else {
		set req.backend = img_director;
	}
		
	if (req.backend.healthy) {
		set req.grace = 30s;
	}
	else {
		set req.grace = 1h;
	}

	if (req.request != "GET" && req.request != "HEAD")
	{
		return (pass);
	}

	return (lookup);
}

sub vcl_pipe {
    # Note that only the first request to the backend will have
    # X-Forwarded-For set.  If you use X-Forwarded-For and want to
    # have it set for all requests, make sure to have:
    # set req.http.connection = "close";
    # here.  It is not set by default as it might break some broken web
    # applications, like IIS with NTLM authentication.
    return (pipe);
}

sub vcl_pass {
    return (pass);
}

sub vcl_miss {
	if (req.http.user-agent ~ "spider" ||
            req.http.user-agent ~ "googlebot" ||
            req.http.user-agent ~ "crawl" ||
            req.http.user-agent ~ "msnbot") {   
            error 503 "Not presently in cache";
        }

return (fetch);
}

sub vcl_fetch
{
        set beresp.ttl = 45d;

	if (beresp.status == 500) {
		set beresp.saintmode = 10s;
		restart;
	}

        set beresp.grace = 30m;

        if (beresp.cacheable) {
                /* marker for vcl_deliver to reset Age: */
                set beresp.http.magicmarker = "1";
        }

        if (req.request == "GET" ) {
                unset beresp.http.Set-Cookie;
                set beresp.ttl = 45d;
                return (deliver);
        }

        if (!beresp.cacheable)
        {
                return (pass);
        }

        if (beresp.http.Set-Cookie)
        {
                return (pass);
        }

        if (beresp.ttl < 24h) {
                # force minimum ttl of 1 hour for all cached objects.
                set beresp.ttl = 45d;
        }

        # We don't want to cache 404. If status differs from 200 and 302
        # then we issue a restart causing varnish to fetch the image again 
        # from the defined director.
        if (beresp.status != 200 && beresp.status != 302) {
                restart;
        }

        return (deliver);
}

sub vcl_deliver {
        set resp.http.Via = "Varnish";

        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-Cache = "HIT";
        }
        else {
                set resp.http.X-Cache = "MISS";
        }

        return (deliver);
}

sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    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>
  </body>
</html>
"};
    return (deliver);
}

