All Linux HowTo's

Using Varnish to “Cap” Server Load – Redhat/CentOS

Share This:

This article demonstrates how to Cap or Limit the load on a Web Server using Varnish. The idea here is to specify a timeout value which, if exceeded, the web surfer will be diverted to a customised error page. By adjusting the timeout value, the administrator can choose what kind of load is acceptable and at what point to start sending visitors to the error page. The error page can explain that “Sorry, the server is under heavy load. Please try again soon!”. In this example, i’ve used the “Director” statement. This means the following script can easily be put into ‘load balancer’ mode. Read this article for more on load balancing with Varnish.

In this example, i’ve used the following settings:
1. Web server IP address of “192.168.122.33”.
2. Domain name of “agix.com.au”.
3. Timeout value of “1 ms”. This is very low for testing.

This is the “/etc/varnish/default.vcl” file used in this example:

#backend default {
#  .host = "127.0.0.1";
#  .port = "80";
#}

#Specify the first server backend.
backend Server1 {
  .host = "192.168.122.33";
  .probe = {
                .url = "/";
                .interval = 5s;
                .timeout = 1 ms;
                .window = 5;
                .threshold = 3;
  }
}

# Specify the servers to balance between. I call this group 'pool1'.
director Pool1 round-robin {
        {
                .backend = Server1;
        }
}
# The rule to use the 'pool1' director - the load balancer.
sub vcl_recv {
   if (req.http.host ~ "^(www.)?agix.com.au$") {
       set req.backend = Pool1;
   }
   # Uncomment to disable caching
   #return(pass);
}

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>We're very sorry, But!.. 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>[enter link description here][1]
"};
    return (deliver);
}

Similar Posts: