vADC Docs

Sending custom error pages

by on ‎02-21-2013 06:56 AM - edited on ‎07-08-2015 11:13 AM by PaulWallace (3,245 Views)

If you are unfortunate enough to suffer a total failure of all of your webservers, all is not lost. Stingray Traffic Manager can host custom error pages for you, and this article shows you how! If all of the servers in a pool have failed, you have several options:

 

  • Each pool can be configured to have a 'Failure Pool'. This is used when all of the nodes in the primary pool have completely failed.
  • You may configure the traffic manager to send an HTTP Redirect message, directing your visitors to an alternate website.

However, you may reach the point where you've got nowhere to send your users. All your servers are down, so failure pools are not an option, and you can't redirect a visitor to a different site for the same reason.

 

In this case, you can use a third option:

 

  • You may configure a custom error page which is returned with every request.

Custom error pages

 

Use the error_file setting in the Virtual Server configuration to specify the response if the back-end servers are not functioning. The error file should be placed in your Extra Files catalog (in the 'miscellaneous files' class:

 

miscfiles.png

 

<html>

   <head>

      <title>Sorry</title>

      <link rel="stylesheet" href="main.css" type="text/css" media="screen" >

   </head>

   <body>

      <img src="logo.gif">

      <h1>Our apologies</h1>

         We're sorry.  All of our operators are busy.  Please try again later.

   </body>

</html>

 

This HTML error page will now be returned whenever an HTTP request is received, and all of your servers are down.

 

Embedding images and other resources

 

Note that the HTML page has embedded images and stylesheets. Where are these files hosted? With the current configuration, the error page will be returned for every request.

 

You can use a little TrafficScript to detect requests for files referenced by the error page, and serve content directly from the conf/extra/ directory.

 

First, we'll modify the error page slightly to may it easier to recognize requests for files used by the error page:

 

<link rel="stylesheet" href="/.extra/main.css" type="text/css" media="screen">

 

and

 

<img src="/.extra/logo.gif">

 

Then, upload the main.css and logo.gif files, and any others you use, to the Extra Files catalog.

 

Finally, the following TrafficScript request rule can detect requests for those files and will make the traffic manager serve the response directly:

 

# Only process requests that begin '/.extra/'

$url = http.getPath();

if( ! string.regexmatch( $url, "^/\\.extra/(.*)$" ) ) {

   break;

} else {

   $file = $1;

}

# If the file does not exist, stop

if( ! resource.exists( $file ) ) break;

# Work out the MIME type of the file from its extension

$mimes = [

   "html"   => "text/html",

   "jpg"    => "image/jpeg",

   "jpeg"   => "image/jpeg",

   "png"    => "image/png",

   "gif"    => "image/gif",

   "js"     => "application/x-javascript",

   "css"    => "text/css"

];

if( string.regexmatch( $file, ".*\\.([^.]+)$" ) ) {

   $mime = $mimes[ $1 ];

}

if( ! $mime ) $mime = "text/plain";

# Serve the file from the conf/extra directory

$contents = resource.get( $file );

http.sendResponse( "200 OK", $mime, $contents, "" );

 

Copy and paste this TrafficScript into the Rules Catalog, and assign it as a request rule to the virtual server. Images (and css or js files) that are placed in the Extra Files catalog can be refered to using /.extra/imagename.png. You will also be able to test your error page by browsing to /.extra/errorpage.html (assuming the file is called errorpage.html in the extra directory).

Contributors