Returning 503s with the maintenance page on Apache-based APIs 0

Posted by stevie
on Sunday, May 18

I spent a good bit of time looking for ways to automatically return a 503 when I ran my capistrano disable_web task, which is a handy way to show your users something intelligible when you have to take your site down for maintenance (and it only works, of course, if you’ve appropriately setup your web config to allow this behaviour). This is fine and great for our animoto.com website, but it doesn’t help our API when it goes down for maintenance.

API consumers care less about a nice looking maintenance page than a correct HTTP return code to let them know that our engine is temporarily down. We told our API clients that we would return an HTTP code of 503 during maintenance periods, but I wasn’t able to find any easy way to have Apache automatically return 503s when the maintenance page was up.

A saw a great EngineYard thread about how automatically return a 503 in the presence of maintenance.html using Nginx, but couldn’t find anything about doing it with Apache. So after doing some hunting around in the Apache docs, I discovered a way to do this without having to use lame techniques that required on-the-fly apache reconfiguring, htaccess tweaking, or a php script.

1
2
3
4
5
6

ErrorDocument 503 /maintenance.html
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !/maintenance.html
RewriteCond %{SCRIPT_FILENAME} !^(.+).(gif|jpg|css|js|swf)$
RewriteRule ^.*$ /maintenance.html [R=503,L]
Comments

Leave a response

Comment