How can I return a 503 status in apache without invoking external scripts

Solution 1:

This serves every request a static holding page along with the 503 status.

RedirectMatch 503 ^/(?!holding\.html)
ErrorDocument 503 /holding.html
Header always set Retry-After "18000"

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).

Solution 2:

Try

Redirect 503 / /

From http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirect :

Other status codes can be returned by giving the numeric status code as the value of status. If the status is between 300 and 399, the URL argument must be present, otherwise it must be omitted. Note that the status must be known to the Apache code (see the function send_error_response in http_protocol.c).

Additional Note: This will display the default/configured ErrorDocument for the 503 Error.


Solution 3:

To make your maintenance mode more flexible, set 503 when a special file exists, ala:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/.maintenance -f
RewriteRule .* - [R=503]

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:

ProxyErrorOverride on
ErrorDocument 503 /error/503.html

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:

  • https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxyerroroverride
  • https://httpd.apache.org/docs/2.4/mod/core.html#errordocument
  • https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
  • https://linux.die.net/man/1/pdsh