ELB Health Check Target for WordPress Networks

A good load balancer health check is succeeds only when the target is up and fails quickly when the target is down, while causing minimal resource usage on the target. When an Amazon Elastic Load Balancer (ELB) is configured with HTTP health checks, there are two things to keep in mind:

  1. The instance internal IP address is used as the host header.
  2. Any response other than HTTP 200 response is considered unhealthy. This includes 301 and 302 redirects.

This causes problems when you have a WordPress multisite network in subdomain mode. If you use “/” as the target, WordPress can’t determine which site to load, and responds with a 302 redirect. Because of (2), the health check fails. The only core file that doesn’t redirect is wp-activate.php, but that still results in a full page render. You could use a static file like readme.html, but then the health check isn’t verifying that PHP is working, let alone WordPress.

I recommend adding a minimal health check target to your theme. In addition to loading faster than a regular page, this also verifies that your theme has been deployed correctly (important if you use an orchestration tool when launching new instances). My sample depends on two assumptions:

1. Apache is configured to send all traffic on a particular port to WordPress, i.e. your httpd.conf looks something like this:

<VirtualHost *:80>
    DocumentRoot /var/www/html/
    ServerName example.com

2. Within DocumentRoot, your WordPress install uses the normal directory structure:

You’ll need to adjust the code if your configuration differs. You can also add additional logic (e.g file_exists()).

Create this file as health-check.php, then set /wp-content/themes/example-theme/health-check.php as the health check Ping Path.

 * Minimal page for ELB HTTP health check.
 * Must return HTTP 200 (not 302) even when no host header is sent. The only
 * known page in Core that does so is wp-activate.php. Using this page instead
 * reduces server load and ensures that the theme exists.
 * @link http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-healthchecks.html
 * @package Example_Theme

$_SERVER['HTTP_HOST'] = 'example.com'; // Use the domain of the network root site.
require( '../../../wp-load.php' );
echo 'OK';

One comment on “ELB Health Check Target for WordPress Networks

Leave a Reply

Your email address will not be published. Required fields are marked *