Creating Custom HTTP Error Pages with Simple HttpErrorPages

Dec 26, 2023 | Programming

Welcome! In this guide, we will navigate through the enchanting realm of generating custom HTTP error pages suitable for a medley of web servers like Nginx, Apache, Lighttpd, Express.js, Koa.js, and Caddy. Experience the joy of crafting personalized error messages that make your users feel cared for, even when things go wrong.

Features Overview

  • Static pages designed for multiple web servers
  • Comprehensive Multi-Language (i18n) support
  • A generator script for easy page customization
  • Compatible with native express.js and koa.js middleware

Getting Started

To begin, download or clone the HttpErrorPages repository. You can either use git or download the pre-built packages, which only include the generated HTML files. There are also various examples for the different HTTP statuses:

Integrating Custom Error Pages

NGINX Integration

For NGINX, you can easily set up custom error pages using multiple error_page directives. Here’s an example configuration:

server {
    listen      80;
    server_name localhost;
    root        /var/www;
    index       index.html;

    location / {
        try_files $uri $uri =404;
    }

    error_page 400 /ErrorPages/HTTP400.html;
    error_page 401 /ErrorPages/HTTP401.html;
    error_page 403 /ErrorPages/HTTP403.html;
    error_page 404 /ErrorPages/HTTP404.html;
    error_page 500 /ErrorPages/HTTP500.html;
    error_page 501 /ErrorPages/HTTP501.html;
    error_page 502 /ErrorPages/HTTP502.html;
    error_page 503 /ErrorPages/HTTP503.html;

    location /ErrorPages {
        alias /var/ErrorPages;
        internal;
}

Think of each error_page directive as a carefully placed signpost on a hiking trail, guiding your users to the right path when something goes awry.

Apache HTTPD Integration

Apache allows similar configurations using the ErrorDocument directive. Here’s how you can set that up:

ErrorDocument 400 /ErrorPages/HTTP400.html
ErrorDocument 401 /ErrorPages/HTTP401.html
ErrorDocument 403 /ErrorPages/HTTP403.html
ErrorDocument 404 /ErrorPages/HTTP404.html
ErrorDocument 500 /ErrorPages/HTTP500.html
ErrorDocument 501 /ErrorPages/HTTP501.html
ErrorDocument 502 /ErrorPages/HTTP502.html
ErrorDocument 503 /ErrorPages/HTTP503.html

Lighttpd Integration

For Lighttpd, simply utilize the server.errorfile-prefix directive to specify error pages:

server.errorfile-prefix = "/var/www/ErrorPages/"

Express.js Integration

To integrate with Express.js, install the http-error-pages package using npm:

yarn add http-error-pages

A simple Express example would look like this:

const express = require('express');
const webapp = express();
const httpErrorPages = require('http-error-pages');

async function bootstrap() {
    await httpErrorPages.express(webapp, {
        lang: 'en_US',
        payload: {
            footer: 'Hello World',
            myvar: 'hello world',
        },
    });
    webapp.listen(8888);
    console.log('Running Demo on Port 8888');
}
bootstrap();

Koa.js Integration

Similarly, integrate with Koa.js by following a parallel structure:

const Koa = require('koa');
const webapp = new Koa();
const httpErrorPages = require('http-error-pages');

webapp.use(await httpErrorPages.koa({
    lang: 'en_US',
    payload: {
        footer: 'Hello World',
    },
}));
webapp.listen(8888);

Caddy Integration

Lastly, for Caddy integration, utilize the errors directive to manage error pages effectively:

errors {
    404 /ErrorPages/HTTP404.html
}

Customization Options

The beauty of the Simple HttpErrorPages project lies in its ability to customize. You can modify styles, change page contents, or even add pages for additional error codes. To get started, clone the repository, install the necessary dependencies, and run the generator script to create your custom error pages.

Troubleshooting

Should you encounter any issues while integrating these custom error pages, here are some troubleshooting ideas:

  • Check the server configuration files for any syntactical errors or missing directives.
  • Ensure the error pages are correctly referenced in your server’s configuration.
  • If pages do not display as expected, validate the path is correctly set and the files exist.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox