Apache and Lighttpd server send error 503 when web site is under maintenance

by on December 27, 2006 · 14 comments· LAST UPDATED August 30, 2007

in , ,

When you are updating website you may need to send error 503 to client. Error 503 indicates that web server is temporary out of service or down. This is useful if you are running popular database driven website such as a forum or e-commerce site. So when the site is under maintenance you can send user a good message indicating that site is down for some work.

This tip will help you to disable a site for maintenance using mod_rewrite without redirecting url.

Both Lighttpd and Apache webserver allows you to send this message to client using server side rewriting using mod_rewrite and php.

Error 503 means

=> Server is too busy to serve your request
=> Server is slashdotted or dugg to death
=> Server is forced to send this message etc

But why to send error 503?

Error 503 informs search engine that site is temporary out of service. This is quite important for site which is heavily depends upon search engine for selling products and services.

PHP code - down.php

The down.php file simply sends a 503 HTTP error header indicating that service is unavailable. echo command sends a message for your site.

So first create a php file as follows:
<?
header("HTTP/1.1 503 Service Unavailable");
echo "Server is down for maintenance. We will be back in 60 minutes.";
?>

Task: Lighttpd send error 503 when web is under maintenance

Copy down.php to webserver documentation root:
cp down.php /home/lighttpd
OR
cp down.php /var/www

Open lighttpd configuration file
# vi /etc/lighttpd/lighttpd.conf
Now add following code:

$HTTP["url"] != "/down.php$" {
   url.rewrite = ( "" => "/down.php" )
}

Save and close the file.

It is also possible to block site for rest of the world except for website developer or site admin IP address:

$HTTP["remoteip"] != "201.203.149.149" {
                   url.rewrite = ( "" => "/down.php" )
}

Restart lighttpd:
# /etc/init.d/lighttpd restart

Task: Apache send error 503 when web is under maintenance

You can use an Apache mod_rewrite module. Open .htaccess file
vi .htaccess
Append following code:
RewriteEngine on
RewriteRule !down.php$ /down.php [L]

Again it is possible to block site for rest of the world except for website developer or site admin IP address:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^201\.203\.149\.149$
RewriteRule !down.php$ /down.php [L]

Save and close file. Restart Apache 2 web server:
# /etc/init.d/httpd restart
OR
# /etc/init.d/apache2 restart

Let us say user hits url http://domain.com/dir/somefile.html
First rules will check for client IP address - 201.203.149.149. If match found user is allowed to browse site. Otherwise user will get 503 error with message:

Server is down for maintenance. We will be back in 60 minutes. 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 14 comments… read them below or add one }

1 Lars Noodén December 29, 2006 at 12:26 pm

Good tips, but if the site is really important then the step on Restarting Apache 2 web server after saving and closing the file needs to verify that the syntax of the configuration file is correct. There are many ways mistakes or typos can be introduced, any of which will bring the server down.

# /etc/init.d/httpd restart
or
# /etc/init.d/apache2 restart

One should do a configuration test first. e.g.

# /etc/init.d/apache2 configtest && \
# /etc/init.d/apache2 reload

or
/usr/sbin/apache2ctl -t -f /etc/apache2.conf && \
/usr/sbin/apache2ctl -f /etc/apach2.conf graceful

Reply

2 nixCraft December 30, 2006 at 1:22 am

Good idea; For lighttpd
lighttpd -t -f /etc/lighttpd.conf && /etc/init.d/lighttpd restart

Appreciate your post.

Reply

3 Daniel January 1, 2007 at 2:49 pm

Do you need to restart Apache after modifying .htaccess? I think not. I you were changing httpd.conf I would agree.

Reply

4 Jerry January 6, 2007 at 10:50 am

I’m just learning to hack .htaccess and use mod_rewrite. It looks to me like this line:

RewriteRule !down.php$ /down.php [L]

only catches URLs to the root directory? How can I redirect all URLs, even URLs to subdirectories deeper in the site, so all URLs get a 503 maintenance page?

Thanks.

Reply

5 nixCraft January 6, 2007 at 11:13 am

Jerry,

No .htaccess rule is correct. If file is not down.php it will display content of down.php for all site urls and sub directories.

Hope this helps.

Reply

6 sangamnath March 30, 2007 at 11:05 am

Hi, i am getting the error 503 when i am importing the data to the client side.i am really not understanding wats the reason behind this.So please can any one help me out to solve this problem.Thanks in advance….

Reply

7 Sagri May 11, 2007 at 10:39 am

Hi,
Thats very efficient way of costimising error pages.

Do you know the way to do the same on tomcat alone with out web server. ie; with out apache.

I use only tomcat 5.5.12 and I want to customise my 503 pages when I am updating.

Thanks in advance.

Reply

8 Carl June 14, 2007 at 1:58 pm

I have tried to use your method, but only the main page and a page one level deeper redirect successfully. If I go any deeper down a valid URL it does not display the site down page.

Reply

9 nixCraft June 15, 2007 at 10:35 am

Do you have other rewrite rules? if so add this rule before those rules

Reply

10 Frederick July 25, 2008 at 8:31 pm

A better .htaccess rule might be:


RewriteEngine On
RewriteCond %{REQUEST_URI} !down.php$
RewriteRule ^(.*)$ /down.php [L]

Reply

11 Anonymous October 25, 2008 at 6:11 pm

Wow, thanks alot! I’ve been wondering how to do this for ages!

Reply

12 Max January 18, 2012 at 5:04 pm

Good idea! In this case, we simply took the site down for a few hours so a 503 probably wasn’t necessary. Great suggestion for longer cases though.

Thanks!

Reply

13 Preet February 7, 2012 at 7:35 am

The "something" can be ascecsed very easily with PHP. If you have a web page address that looks like this:index.php? month=september&name=smith&age=24Then you can access those values in PHP like this:<?phpecho $_GET('month');echo $_GET('name');echo $_GET('age');?>This will print septembersmith24.As for accessing a text file, that's easy too:<?php$text = file_get_contents( 'textfile.txt' );echo $text;?>This will read the entire contents of the file textfile.txt into the variable $text, and then print it out for you.You can also access many databases with PHP, I highly recommend you use MySQL. It's a bit too complicated to describe here, though. There are some good references made to good books by folks above. The "Teach Yourself" series is great.

Reply

14 Steve March 29, 2012 at 7:50 pm

Weird thing. I only see the echo message once. The next time I visit the site there’s not echo message just a header message? Any thoughts as to why this would happen. It’s returning a 503 message every time, it’s just not returning the echo’d message.

Thanks!

Reply

Leave a Comment

Tagged as: , , , , , , , , , ,

Previous post:

Next post: