Using Varnish to set a Maintenance Page on your website

There may come a time when you would like to perform a fairly major update to your website, but you don’t want to be serving out semi-broken pages while you update your stylesheets, javascript files or static image assets. Without re-configuring Apache to point to an alternative DocumentRoot, how might you display a simple maintenance page to avoid this? What if you need to take Apache offline (even momentarily) to update some of the dependency modules? Or what if you finally want to do that upgrade from Lighttpd to Apache? What will serve out your Maintenance Page while your webserver is offline?

If you use an HTTP accelerator like Varnish, you can use it to serve out a simple Maintenance Page whilst you do your updates in the background. Instead of passing through incoming requests to your Apache back-end, and then caching them for subsequent requests, Varnish will simply respond with the Maintenance Page that you design.

Sound like something you might want to do? Well, here’s how (on Red Hat and clones):

Create a Varnish VCL configuration file somewhere on your Linux system. My default Varnish configuration file lives in /etc/varnish/default.vcl, so I chose to put my new Varnish Maintenance configuration file at /etc/varnish/maintenance.vcl. This new file should have the following contents (you may need to change small details like the backend host and port to match your specific configuration):

# /etc/varnish/maintenance.vcl
backend default {
    .host = "127.0.0.1";
    .port = "8000";
}
 
sub vcl_recv {
    error 503;
}
 
sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    # Create your Maintenance Page here
    synthetic {"We're currently down for maintenance.

We’re currently down for maintenance.

We won’t be long, so please check back soon. Thanks for your patience.

 
    "};
    return (deliver);
}

This file, /etc/varnish/maintenance.vcl, can live on your filesystem without ever impacting the day-to-day operation of your website. Varnish will still load its default configuration on startup, and it will only use your new Maintenance configuration if you tell it to.

So, how do you tell it to? When you’re ready to start your website updates and want to start displaying your Maintenance Page, you simply log onto your server and run the following commands:

varnishadm vcl.load maintenance_mode /etc/varnish/maintenance.vcl

This will create a new Varnish profile, called maintenance_mode. Note that at the moment, Varnish is still serving normal website content; all you’ve done is made it aware of an alternative configuration to the one it normally uses.

varnishadm vcl.use maintenance_mode

Now we’ve activated the new maintenance_mode configuration profile. You should find that if you attempt to load your webpage, Varnish returns your Maintenance Page. You should find that there are no hits in the Apache web logs, and you should also find that if you hit Apache directly, you will see your regular website (but you’ve got a firewall in place to stop every Tom, Dick and Harry doing that, right?). If you’ve got multiple hosts serving the same website content through a load-balancer, you’ll obviously need to run the commands above on each webserver separately.

You’re now ready to start your maintenance. You can take as long as you need at this point, because Varnish has your back. If you’re going to be a while, I would mention an estimated time when you expect your website to be back online on the Maintenance Page, to prevent people getting annoyed if they keep checking back before your new website is back up.

You can stop and start services to your heart’s content, with the obvious exception of Varnish! You can pretty do whatever you want behind the scenes, as Varnish won’t pass any traffic back to Apache.

Once you’ve finished making your updates, and you’re ready to launch your shiny new website, you simply run the following command:

varnishadm vcl.use boot

This will revert Varnish back to it’s default (startup) configuration, and you should find that it starts loading your updated website just as it did previously.

Hope you found that useful!

Max’s First Birthday


As some of you may know, my girlfriend and I have a Bengal Cat called Max. It just so happens that today, February 19th, is Max’s first birthday. I thought it would be good to show his first year in photos, so here it is:

Max at 7 weeks, before we brought him home

Max at 9 weeks, a few minutes after he arrived in our home:

Still at 9 weeks, now settled in:

Sunbathing after a hard day’s play:

Figuring out computers:

Contortionist!

Wonder what’s out there…

Ever since he was small, he’s always sat with such good posture!

Hungreeeee!

At one year old, Max is now a 7kg monster!

First stumbling block

So, my girlfriend and her family have popped out for the afternoon, and I’m home alone. I figured now would be a good time to start working on one of those resolutions – learning Mandarin. But lo and behold, technical difficulties strike almost immediately!

I guess I haven’t actually used Rosetta Stone since switching to my MacBook Air (but I did do a full installation as preparation). When I launched the app, the first thing I went to do was configure the microphone – but there are no microphones listed inside the application. That’s not right! At the time I had my (new :-)) Bose headphones with in-line mic and volume controls – the Mac recognised the mic, and I was getting audio feedback through the headphones, so everything seemed to be working correctly. So it looks like it might just be Rosetta Stone that doesn’t like this setup.

A quick bit of Googling shows that this seems to be a common issue on the 2011 MacBook Air, albeit one without a definitive answer. For now, I’m trying to update the application from v3.4.5 to v3.4.7. Unfortunately, downloads take forever in China, so it’ll be a while before I find out whether this works or not.

I’ll post back once I’ve got something more to tell 🙂

New Year’s Resolutions

I’m not sure I really believe in New Year’s resolutions, but it seems like a good time every year to reflect on the previous year, and see what I can change in my life for the better.

Unfortunately, I seem to be pretty bad at keeping resolutions I make, so my New Year’s resolutions this year will be very similar to those I made last year. And the year before that. And maybe even the same as 2009. They would be:

  • Get in shape/lose weight/eat healthier/exercise more
  • Learn Mandarin Chinese (my girlfriend is Chinese, so this is important for the longevity of our relationship)
  • Write a blog

Unfortunately, I’ve failed at all three of these things the past three years. Every year I start a blog, hit the gym, and start learning Chinese; all with a vengeance. But then it peters out.

I figure this year, I’ll use the blog to keep track of the other two activities. Hopefully at least one of them will give me something interesting to write about on a regular basis!

Summing up 2011, looking forward to 2012

Looking back on 2011, it’s been an interesting year. It’s no wonder I sit here at the end of the year feeling shattered, and wondering why I was so busy all year! Here’s some of the stuff that kept me busy this year:

  • Saw the New Year in in Shanghai with my girlfriend’s parents
  • Watched 100 Rolls Royces parade through London to celebrate 100 years of the Rolls Royce motor company
  • Bought a cat (his name is Max, and he’s a Bengal Cat, in case you’re interested)
  • Watched the Royal Wedding (and enjoyed the additional holiday)
  • Played host to a number of family visits from abroad
  • Visited Windsor
  • Visited (Republic of) Ireland
  • Visited Chicago for some company training
  • Visited New York for leisure (and some hurricane spotting)
  • Went to the Chelsea Auto Legends car show
  • Visited Mexico (and consumed far too much Tequila!)
  • Visited Edinburgh
  • Returned to China for the Christmas break; visited Shanghai, Hainan and Hong Kong; saw the New Year in in Shanghai again

I’m sure there are many more things that happened this year that I’ve simply forgotten about, but were no less deserving of being on the list. Looking forward, there are a number of things I’m looking forward to in 2012:

  • Visiting India for work
  • The start of the Chinese Year of the Dragon
  • Celebrating my girlfriend’s dad’s 60th birthday
  • Visiting Indonesia for a friend’s wedding
  • The launch of the YouView set-top box
  • The Queen’s Diamond Jubilee
  • The London Olympics

I hope everyone reading this has enjoyed 2011, and that they have a prosperous new year in 2012!