I need to return a 503 status code from one of my sites while it's down for maintenance, in the time-honoured SE_firendly fashion. I can't seem to work out how to do this without invoking external scripts, which I'd rather avoid.
Is there an apache directive which will allow me to return an arbitrary HTTP status code without resorting to hacks like invoking a php script which sets the status header?
This serves every request a static holding page along with the 503 status.
RedirectMatch is used to negate the holding page itself which would otherwise create an infinite loop.
mod_header is used to set a Retry-After header so that you can tell Google/other bots etc that you should back up after 18000 seconds (5 hours) in this example. You can
sudo ap2enmod header
to activate mod_header (which is required for the Header directive).Try
From https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect :
Additional Note: This will display the default/configured ErrorDocument for the 503 Error.
To make your maintenance mode more flexible, set 503 when a special file exists, ala:
NOTE: The location of DOCUMENT_ROOT can differ between httpd 2.2 and 2.4
This way all you need to do is "touch /path/to/docroot/.maintenance" and your site will instantly start returning 503's. Then to make it all purdy, create a custom nicely formatted html error page and add this to the correct apache server's config file:
And for bonus points if you have multiple web servers either make the location of .maintenance on a shared filesystem, or for even more bonus points:
pdsh -w $web1_ip,$web2_ip,$web3_ip "sudo touch /path/to/docroot/.maintenance"
The advantage of making the maintenance mode file based is that it works when your application doesn't, it's quick and simple, and it's easier for a frontend caching proxy to handle because you can handle all backend 503's in one fell swoop.
Sources: