How to Create Custom 503 Maintenance Page

By: Sunil Kumar |  In: Server  |  Last Updated: 2018/09/22

How to Create Custom 503 Maintenance Page

HTTP 503 response is temporary service not available error. Whether it is due to some technical error or maintenance purpose.
So a 503 error let your visitors know that there are some technical issues with the website or the website is under maintenance.
In Nginx, you can customize this page to let your users know what exactly is happening on the server.

Create a HTTP 503 maintenance page

Create a 503.html file into your domain’s root directory

nano -w /var/www/domain1.com/503.html

And paste following code inside it

<html>
   <head>
     <title>Error 503 Service Unavailable</title>
   </head>
   <body>
     <h1>Temporarily down for maintenance</h1>
     The site is down for scheduled maintenance. Sorry for the inconvenience. Will be back around 5:30PM.
   </body>
</html>

Update Nginx Configuration to serve this 503 page

Place this simple configuration inside your server block file(inside server{} block).

 if (-f $document_root/503.html) {
   return 503;
  }
 error_page 503 @maintenance;
 location @maintenance {
   rewrite ^(.*)$ /503.html break;
 }

This is a simple if condition which tells Nginx that if a 503 occurs on the system, serve the error page from this file(503.html).

Save and close the file and reload the Nginx server

sudo service nginx reload

Serve maintenance page to visitors but the real content to the internal users

Sometimes you want to serve the error page to your visitors but the real content to the internal users.

Don’t worry.

You can tell Nginx to do that

You can check the IP of user and serve accordingly

# skip our office router ip or webmaster ip 1.1.1.1
if ($remote_addr != "1.1.1.1") {
   return 503;
}
error_page 503 @maintenance;
location @maintenance {
  rewrite ^(.*)$ /error503.html break;
}

Using Geo IP Modules To Skip Certain IPs or Subnets

If you’ve Geo IP module installed under nginx, you could add the following in the global section:

## add in global section
geo $maintenance {
 default 0;
 1.1.1.1/28 0; # allow office subnet to skip http 503 mode
 192.9.2.4 0; # allow webmaster remote ip to skip http 503 mode
}
## add in server section
location / {
 if ($maintenance) {
  return 503;
 }
}
error_page 503 @maintenance;
location @maintenance {
 rewrite ^(.*)$ /error503.html break;
}

To turn on maintenance set default from 0 to 1:

default 0;

to

default 1;

And reload the Nginx server.

sudo service nginx reload

Back to Normal- Remove maintenance mode

Open the Nginx configuration file and comment out or remove the maintenance related code

# skip our office router ip or webmaster ip
###if ($remote_addr != "1.1.1.1") {
### return 503;
### }

OR if you are using Geo IP module set default from 1 to 0:

default 0;

Finally, reload the Nginx web server.

Comments


Leave a Comment

Your email address will not be published.

*


Sunil Kumar


I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Join more than 10,000 others Web Developers


%d bloggers like this: