Static custom 404 page, Nginx and Apache

Written by
Date: 2013-05-18 10:07:13 00:00

Spanish version

When you run a dynamic website, like one powered by PHP, Django or other similar, you have the option to manage the 404 (Not Found) pages very efficiently. Your application will take care of that. But, when you are working with static websites, you do not have an application running behind to take care of that.

In that case the webserver has to deal with 404 "page not found" errors. Let's see how to configure the two most used webserver to use a custom 404 webpage.

First things firts. We need to create the 404 page, let's call it… well 404.html

<html>
<title>File Not Found</title>
<h1>Oops, we do not find what you are looking for</h1>
<p>It seems that we have put that page in a place we can't remember, as we can not find it.</p>
<p>If you are sure that the page you are looking for should be here please contact the webmaster, or go to our <a href="/">home pagez/a> and continue from there</p>
</html>

OK, I know you can do better, but this is just a simple model.

Configure Apache for custom 404 page

Open the file apache2.conf or httpd.conf which is usually located at /etc/apache2/ or /etc/httpd/conf/. Once there you can add this line.

 ErrorDocument 404 /404.html

That is it, Apache will now serve 404.html page when there is a 404 error.

Configure Nginx for custom 404 page

If you are working with nginx, it is just as simple as with Apache, once again we need to find the configuration file of the Nginx server. This file is usually located at /etc/nginx/nginx.conf, /etc/nginx/conf/nginx.conf.

Just add this line:

 error_page   404  =  /404.html;

But, it is recommended to use this small coding instead of just that line.

 error_page 404 /404.html;
 location  /404.html {
   internal;
   }

This way the users will not be able to browse the 404 page directly.