hoangweb
6/18/2016 - 1:34 AM

varnish/myvarnish.vcl

varnish/myvarnish.vcl

# Required for VCL 4.0
vcl 4.0;
# Import VMod's
import std;

acl purge { # Who is allowed to purge?
    # Web server with plugin which will issue PURGE requests
    "localhost";
    "127.0.0.1";
    "wheezy.dimitri.eu";      # Just my server its hostname
}

sub vcl_recv {
    
  # admin users always miss the cache
    if( req.url ~ "^/wp-(login|admin)" || req.url ~ "^/mylogin.php" ||
        req.http.Cookie ~ "wordpress_logged_in_" ){
            return (pass);
    }
    # Don't cache backend
    if (req.url ~ "wp-(login|admin|comments-post.php|cron.php)" || req.url ~ "mylogin.php") {
        return (pass);
    }

    if (std.healthy(resp.backend_hint)) {
        set req.grace = 2h;
        set req.ttl = 5s;
      } else {
        # Accept serving stale object (extend TTL by 6h)
        # Allow the backend to serve up stale content if it is responding slowly.
        set req.grace = 6h;
    }
    # Do not allow outside access to cron.php or install.php.
    #if (req.url ~ "^/(cron|install)\.php$" ) {
        # Have Varnish throw the error directly.
    #    error 404 "Page not found.";
        # Use a custom error page that you've defined in Drupal at the path "404".
        # set req.url = "/404";
    #}
    # Normalize the header, remove the port (in case you're testing this on various TCP ports)
    set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");

    # Check if we may purge (only localhost)
    if (req.method == "PURGE") {
        if (!client.ip ~ purge) {
            #error 405 "This IP is not allowed to send PURGE requests"; # v3
            return( synth(405, "This IP is not allowed to send PURGE requests"));
        }
        ban("req.url ~ ^" + req.url + "$ && req.http.host == " + req.http.host);
        #Below is used with Varnish version < 3.0. Comment out ban above if using those versions
        #purge("req.url ~ ^" req.url "$ && req.http.host == "req.http.host);
        return(lookup);
    }

    if (req.method != "GET" &&
        req.method != "HEAD" &&
        req.method != "PUT" &&
        req.method != "POST" &&
        req.method != "TRACE" &&
        req.method != "OPTIONS" &&
        req.method != "DELETE") {
            # /* Non-RFC2616 or CONNECT which is weird. */
            return (pipe);
    }
    if (req.method != "GET" && req.method != "HEAD") {
    # We only deal with GET and HEAD by default
        return (pass);
    }
    if (req.method != "GET" && req.method != "HEAD") {
    # We only deal with GET and HEAD by default
        return (pass);
    }

    ## Modified from default to allow caching if cookies are set, but not http auth
    if (req.http.Authorization) {
        /* Not cacheable by default */
        return (pass);
    }
    # remove Cookies (ingore cookie for static page)
    if (req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|js|css|txt|zip)(\?.*|)$") {
        unset req.http.cookie;
    }

    # remove Cookies on home page
    if (req.url ~ "^/$") {
        unset req.http.cookie;
    }

    # Define the default grace period to serve cached content
    # Allow a grace period for offering "stale" data in case backend lags
    set req.grace = 30s;

    unset req.http.X-Forwarded-For;
    set req.http.X-Forwarded-For = client.ip;

#std.log("hoangweb_log:" + req.http.cookie);

  
 if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
   unset req.http.cookie;
   set req.url = regsub(req.url, "\?.*$", "");
 }
 if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
   set req.url = regsub(req.url, "\?.*$", "");
 }
 if (req.http.cookie) {
   if (req.http.cookie ~ "(wp-settings-)") {
     return(pass);
   } else {
     unset req.http.cookie;
   }
  }

  
    # Check the cookies for wordpress-specific items
    #if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
    #    return (pass);
    #}

    # Remove cookies set by Google Analytics (pattern: '__utmABC')
    if (req.http.Cookie) {
        set req.http.Cookie = regsuball(req.http.Cookie,
            "(^|; ) *__utm.=[^;]+;? *", "\1");
        if (req.http.Cookie == "") {
            unset req.http.Cookie;      # or remove
        }
    }
    
    # Set client IP
    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;
    }
    
    # always pass through POST requests and those with basic auth
    if (req.http.Authorization || req.method == "POST") {
        return (pass);
    }
    # Did not cache HTTP authentication and HTTP Cookie
    if (req.http.Authorization || req.http.Cookie) {
        # Not cacheable by default
        return (pass);
    }
    
    # Do not cache these paths
    if (req.url ~ "^/wp-cron\.php$" ||
        req.url ~ "^/xmlrpc\.php$" ||
        req.url ~ "^/wp-admin/.*$" ||
        req.url ~ "^/wp-includes/.*$" ||
        req.url ~ "\?s=") {
            return (pass);
    }
    
    # ignore cookie for product page, it might pesudo static page
    if (req.url ~ "^/[^?]+-product-?+\.(html)(\?.*|)$") {
        unset req.http.cookie;
    }
    # remove Cookies on home page
    if (req.url ~ "^/$") {
        unset req.http.cookie;
    }
    # 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, "\?.*$", "");
    }
    if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
       set req.url = regsub(req.url, "\?.*$", "");
    }
    
    # Force lookup if the request is a no-cache request from the client
#   if (req.http.Cache-Control ~ "no-cache") {
#       return (pass);
#   }

    # Define the default grace period to serve cached content
    # Allow a grace period for offering "stale" data in case backend lags
    set req.grace = 30s;

    unset req.http.X-Forwarded-For;
    set req.http.X-Forwarded-For = client.ip;
    
    if (req.http.cookie) {
       if (req.http.cookie ~ "(wp-settings-)") {
         return(pass);
       } else {
         unset req.http.cookie;
       }
    }
    
    # Remove the "has_js" cookie and Google Analytics cookies.
    set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");

    # 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.=[^;]+(; )?", "");

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

    # Remove the wp-settings-time-1 cookie
    set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

    # Remove the wp test cookie
    set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
    
    # Are there cookies left with only spaces or that are empty?
    if (req.http.cookie ~ "^ *$" || req.http.Cookie == "" || req.http.Cookie ~ "^\s*$") {
        unset req.http.cookie;
    }
    
    
    # Did not cache the RSS feed
    if (req.url ~ "/feed") {
        return (pass);
    }
    # Blitz hack
    if (req.url ~ "/mu-.*") {
            return (pass);
    }
    ## Pass php page
    if (req.url ~ ".php") {
        return (pass);
    }
    
    # By ignoring any other cookies, it is now ok to get a page
    #unset req.http.Cookie;
    # Use anonymous, cached pages if all backends are down.
    if (!std.healthy(resp.backend_hint)) {     # If all web servers go down, this flag becomes FALSE
        unset req.http.Cookie;
    }
    
}

sub vcl_backend_response {
  
    # remove some headers we never want to see
    unset beresp.http.Server;
    unset beresp.http.X-Powered-By;
    
    # keep all objects for 6h beyond their TTL
    # Allow items to be stale if needed.
    set beresp.grace = 6h;

#std.log("hoangweb_log: status->" + beresp.status);
    if (beresp.ttl < 3600s) {
        set beresp.ttl = 3600s;
    }
    if (req.url ~ "wp-(login|admin)" || req.url ~ "mylogin.php" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
      return (hit_for_pass);
    }
    # If we fetch a 500, serve stale content instead
    if (  beresp.status == 500 || beresp.status == 502 || beresp.status == 503) {
        set beresp.ttl = 0s;
        set beresp.saintmode = 100s;
        return(restart);
    }

    # do not cache blank pages
    if (beresp.http.Content-Length == "0"){
        return(restart);
    }
    #if( beresp.status>=500) {
    #    set beresp.ttl = 0s;
    #    return(restart);
    #}
   
  if ( (!(req.url ~ "(wp-(login|admin)|login|mylogin.php)")) || (req.method == "GET") ) {
    #unset beresp.http.set-cookie;
  }
  #if (beresp.ttl && !beresp.http.cache-control) {
   #   set beresp.ttl = 600s;
   #   set beresp.http.cache-control = "max-age=600";
   # }
  # For static content strip all backend cookies
    if (req.url ~ "\.(css|js|png|gif|jp(e?)g)|swf|ico") {
        unset beresp.http.cookie;
    }
    # only allow cookies to be set if we're in admin area
    #if( beresp.http.Set-Cookie && req.url !~ "^/wp-(login|admin|mylogin.php)" ){
    #    unset beresp.http.Set-Cookie;
    #}
    if ( (!(req.url ~ "(wp-(login|admin|comments-post.php|cron.php)|login|mylogin.php)")) || (req.method == "GET") ) {
        #unset beresp.http.set-cookie;
        set beresp.ttl = 4h;
    }
    # don't cache response to posted requests or those with basic auth
    if ( req.method == "POST" || req.http.Authorization ) {
        return (hit_for_pass);
    }
    # don't cache search results
    if( req.url ~ "\?s=" ){
        return (hit_for_pass);
    }
    
  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;
  #}
    # Cache the following files extensions 
    if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
        unset req.http.cookie;
    }
    
    # only cache status ok
    if ( beresp.status != 200 ) {
        return (hit_for_pass);
    }
    
    # If our backend returns 5xx status this will reset the grace time
    # set in vcl_recv so that cached content will be served and 
    # the unhealthy backend will not be hammered by requests
    if (beresp.status == 500) {
        set beresp.grace = 60s;
        return (restart);
    }
    if (!beresp.http.cache-control) {
      set beresp.ttl = 600s;
      set beresp.http.cache-control = "max-age=600";
    }
    # GZip the cached content if possible
    if (beresp.http.content-type ~ "text") {
        set beresp.do_gzip = true;
    }
 
    # if nothing abovce matched it is now ok to cache the response
    set beresp.ttl = 24h;
    return (deliver);
}

sub vcl_deliver {

# multi-server webfarm? set a variable here so you can check
# the headers to see which frontend served the request
#   set resp.http.X-Server = "server-01";
   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;  # remove X-Varnish
    
    # Remove some headers: PHP version
    unset resp.http.X-Powered-By;
    
    # Remove some headers: Apache version & OS
    unset resp.http.Server;
    
    if(req.restarts > 0){
        std.log("Log: The URL " + req.url + " has been restarted " + req.restarts + " times");
    }
    if(req.http.x-no-content == "1"){
        if( std.integer(resp.http.Content-Length,0) > 50 ){
            std.log("Log: The URL " +  req.http.host + req.url + " was blank on first load, but now has a content length of " + resp.http.Content-Length);
            set req.http.x-cacheable = 1;
            return(restart);
        }
    
        if( std.integer(resp.http.Content-Length,0) < 50 ){
            std.log("Log: The URL " + req.http.host + req.url + " was blank on first load, and now is still blank " + resp.http.Content-Length);
        }
    }
}

sub vcl_hit {
    if (req.method == "PURGE") {
        purge;
        set obj.ttl = 0s;
        #error 200 "Purged.";
        return( synth(200, "Purged"));
    }
    return (deliver);
}
sub vcl_miss {
    # Set up invalidation of the cache so purging gets done properly
    if (req.method == "PURGE") {
        purge;
        return( synth(200, "Purged"));
    }
    return (fetch);
}


sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    synthetic {"
<!DOCTYPE HTML>
<html>
<head>
<title>Trang loi 404 !</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<style>
a{text-decoration:none;}
.txt-rt{text-align:right;}/* text align right */
.txt-lt{text-align:left;}/* text align left */
.txt-center{text-align:center;}/* text align center */
.float-rt{float:right;}/* float right */
.float-lt{float:left;}/* float left */
.clear{clear:both;}/* clear float */
.pos-relative{position:relative;}/* Position Relative */
.pos-absolute{position:absolute;}/* Position Absolute */
.vertical-base{ vertical-align:baseline;}/* vertical align baseline */
.vertical-top{  vertical-align:top;}/* vertical align top */
.underline{ padding-bottom:5px; border-bottom: 1px solid #eee; margin:0 0 20px 0;}/* Add 5px bottom padding and a underline */
nav.vertical ul li{ display:block;}/* vertical menu */
nav.horizontal ul li{ display: inline-block;}/* horizontal menu */
img{max-width:100%;}
/*end reset*/
/*-----light-font----*/
/* get the required local files */
@font-face {
  font-family: 'Roboto';
  src: url('roboto.eot'); /* IE9 Compat Modes */
  src: url('roboto.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
  url('roboto.woff') format('woff'), /* Modern Browsers */
  url('roboto.ttf')  format('truetype'), /* Safari, Android, iOS */
  url('roboto.svg#svgFontName') format('svg'); /* Legacy iOS */
}
body {
  background:url(http://i.imgur.com/0svo7cM.jpg) no-repeat 100%;
  background-size: 100%;
  font-family: 'Roboto', sans-serif;
  font-size: 100%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
}
/**-----start-wrap---------**/
.wrap
{
  width:70%;
  margin:5.2% auto 4% auto;
}
/**-----start-logo--------**/
.logo
{
  padding: 1em;
  text-align: center;
  padding: 1% 1% 5% 1%;
}
.logo h1{
  display: block;
  padding: 2em 0em;
}
.logo span{
  font-size: 2em;
  color:#fff;
}
.logo span img{
  width:40px;
  height: 40px;
  vertical-align:bottom;
  margin: 0px 10px;
}
/**-----end-logo---------**/
/**-----start-search-bar-section------**/
.buttom
{
  background:url(http://i.imgur.com/xl77A80.png) no-repeat 100% 0%;
  background-size: 100%;
  text-align: center;
  vertical-align: middle;
  margin: 0 auto;
  width: 556px;
}
.seach_bar
{
  padding:2em;
}
.seach_bar p{
  font-size: 1.5em;
  color:#fff;
  font-weight: 300;
  margin: 2.6em 0em 0.9em 0em;
}
.seach_bar span a{
  font-size: 1em;
  color:#fff;
  text-decoration:underline;
  font-weight: 300;
  font-family: 'Roboto', sans-serif;
}
/**********search_box*************/
.search_box{
  background: #F1F3F6;
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
  padding: 6px 10px;
  position: relative;
  cursor: pointer;
  width: 75%;
  margin: 0 auto;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  -o-border-radius: 5px;
  box-shadow: inset 0 0 5px rgba(156, 156, 156, 0.75);
  -moz-box-shadow: inset 0 0 5px rgba(156, 156, 156, 0.75);
  -webkit-box-shadow: inset 0px 0px 5px rgba(156, 156, 156, 0.75);
}
.search_box img {
  vertical-align: middle;
  margin-right: 10px;
}
.search_box form input[type="text"] {
  border: none;
  outline: none;
  background: none;
  font-size: 1em;
  color:#999;
  width:100%;
   font-family: 'Roboto', sans-serif;
  -webkit-apperance: none;
}
.search_box form input[type="submit"] {
  border: none;
  cursor: pointer;
  background: url(http://i.imgur.com/2qznxqx.png) no-repeat 0px 1px;
  position: absolute;
  right: 0;
  width: 34px;
  height: 25px;
  outline: none;
  -webkit-appearance:none;
}
/*****copy-right*****/
.copy_right  {
  color: #fff;
  font-size: 0.85em;
  line-height: 1.8em;
  padding: 5em 0px 0px 0px;
  font-family: 'Roboto', sans-serif;
  text-align: center;
}
.copy_right a {
  color:#e14d43;
  -webkit-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -ms-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}
.copy_right a:hover {
  color:#fff;
}
/*********Media Queries************/
@media only screen and (max-width: 768px)
{
.wrap {
  width: 80%;
} 
.logo img {
  width: 315px;
}
}
@media only screen and (max-width: 640px)
{
.wrap {
  width: 85%;
} 
.logo {
  padding: 1% 1% 12% 1%;
}
.buttom {
  width: 515px;
}
.logo img {
  width: 300px;
}
}
@media only screen and (max-width: 480px)
{
.wrap {
  width: 90%;
} 
.logo {
  padding: 1% 1% 12% 1%;
}
.buttom {
  width: 440px;
}
/***/
.logo span {
  font-size: 1.6em;
}
.seach_bar p {
  font-size: 1.2em;
  margin: 2.6em 0em 0.7em 0em;
}
.search_box {
  padding: 3px 10px;
}
.logo img {
  width: 270px;
}
}
@media only screen and (max-width: 320px)
{
.wrap {
  width: 90%;
} 
.logo {
  padding: 1% 1% 12% 1%;
}
.buttom {
  width: 290px;
}
/***/
.logo span {
  font-size: 1.4em;
}
.seach_bar p {
  font-size: 1em;
  margin: 1.5em 0em 2em 0em;
}
.logo span img {
vertical-align: middle;
}
.logo img {
  width: 200px;
}
.copy_right {
  padding: 2em 0px 0px 0px;
}
}
</style>
</head>
<body>
  <!-----start-wrap--------->
  <div class="wrap">
    <!-----start-content--------->
    <div class="content">
      <!-----start-logo--------->
      <div class="logo">
        <h1><a href="http://www.hoangweb.com/"><img src="http://i.imgur.com/afU3Cqs.png"/></a></h1>
        <span><img src="http://i.imgur.com/8S6vcKa.png"/>Xin loi! Vui long khong truy cap qua nhanh !</span>
      </div>
      <!-----end-logo--------->
      <!-----start-search-bar-section--------->
      <div class="buttom">
        <div class="seach_bar">
          <p>Hay quay ve <span><a href="http://www.hoangweb.com/">trang chu</a></span> hoac tim kiem tai day</p>
          <!-----start-sear-box--------->
          <div class="search_box">
          <form>
             <input type="text" value="Tim kiem" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Tim kiem';}"><input type="submit" value="">
            </form>
           </div>
        </div>
      </div>
      <!-----end-sear-bar--------->
    </div>
    <!----copy-right-------------->
  <p class="copy_right">Copyright &#169; 2016 Cong ty TNHH Hoang Web | All Rights Reserved - <a href="http://www.hoangweb.com" target="_blank">&nbsp;www.Hoangweb.com</a> </p>
  </div>
  
  <!---------end-wrap---------->
</body>
</html>
"};

    if (obj.status == 503) {
        # set obj.http.location = req.http.Location;
        set obj.status = 404;
        set obj.response = "Not Found";
        return (deliver);
    }
}
sub vcl_init {
    return (ok);
}

sub vcl_fini {
    return (ok);
}