How to Configure Custom Error Pages in Nginx/Apache

Default web server error pages are generic and unbranded. Custom error pages improve user experience during outages or broken links, and can guide visitors back to useful content instead of a dead end.

Why Custom Error Pages Matter

A well-designed 404 page can retain a visitor who hit a broken link (by offering search or navigation back to your site) rather than simply displaying a bare "Not Found" and losing them entirely.

Common Error Codes Worth Customizing

CodeMeaning
404Page not found
500Internal server error
502/503/504Backend/gateway errors, often during deployments or outages
403Forbidden/access denied

Step 1 — Create Your Custom Error Page(s)

sudo mkdir -p /var/www/errors
sudo nano /var/www/errors/404.html

Design a page matching your site's branding, ideally including navigation links and perhaps a search box to help lost visitors find what they need.

Step 2 — Configure Nginx

server {
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /404.html {
        root /var/www/errors;
        internal;
    }

    location = /50x.html {
        root /var/www/errors;
        internal;
    }
}

internal prevents these error page URLs from being directly accessible by visitors, reserving them purely for error handling.

Step 3 — Configure Apache

ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

Step 4 — Test

curl -I https://yourdomain.com/this-page-does-not-exist

Verify a 404 status is returned, then visit the URL in a browser to confirm your custom page displays correctly.

Handling 502/503/504 Pages for Backend Outages

These pages are especially important since they appear during actual outages/deployments — keep them simple, static (not dependent on any backend that might itself be down), and reassuring, letting visitors know the issue is known and being addressed.

Making Error Pages Genuinely Static

Ensure custom error pages don't depend on your application/database being available — a 500-error page that itself requires a database connection is useless precisely when it's needed most (during a database outage).

Including Useful Content on the 404 Page

  • A search box or link to search functionality
  • Links to popular/main site sections
  • A brief, friendly explanation rather than technical jargon

SEO Considerations for 404 Pages

Ensure your custom 404 page still returns an actual 404 HTTP status code (not 200) — a "soft 404" (custom page content but with a 200 status) confuses search engines into potentially indexing broken URLs as if they were valid content.

Common Errors

Custom error page doesn't display, default page shows instead — verify the file path in the configuration exactly matches where the custom page actually exists, and check for typos in the error_page/ErrorDocument directive.

Continue Reading

Browse more articles in Web Servers.

  • custom error pages, nginx 404 page, apache errordocument, custom 500 page
  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource...

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...