Custom error pages explain why a request failed and help visitors find another page. This guide configures static pages for missing pages and server errors on a Linux server running Apache or Nginx.
Why custom error pages matter
Keep the original error status, such as 404 or 500. Showing a friendly page with a success status can hide faults from monitoring and search engines.
Create custom error page files
Create 404.html and 500.html in the site’s actual document root. Use simple HTML with a short explanation and a link to the home page. Avoid PHP or other application dependencies that might also fail.
Example 404.html:
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Page not found</title></head>
<body><h1>Page not found</h1><p>Check the address or return to the <a href="/">home page</a>.</p></body>
</html>Create a separate 500.html explaining that the request could not be completed and suggesting that the visitor try later.
Configure custom error pages in Apache
Back up the site’s virtual-host configuration. Within its existing VirtualHost block, add:
ErrorDocument 404 /404.html
ErrorDocument 500 /500.htmlOn Debian or Ubuntu, check the configuration and reload only after the check succeeds:
sudo apache2ctl configtest
sudo systemctl reload apache2Other distributions may use httpd. Use the site’s actual configuration and service name. On a control-panel server, use supported custom configuration rather than editing generated files.
Configure custom error pages in Nginx
Within the site’s existing server block, add the following, replacing /var/www/html with its document root:
error_page 404 /404.html;
error_page 500 502 503 504 /500.html;
location = /404.html {
root /var/www/html;
internal;
}
location = /500.html {
root /var/www/html;
internal;
}Check the configuration, then reload if it passes:
sudo nginx -t
sudo systemctl reload nginxIf errors come from an upstream application, review proxy_intercept_errors or fastcgi_intercept_errors for that location. Static error configuration alone does not override every upstream response.
Set file permissions
Ensure the server can read the two files and traverse their parent directories. Ordinary static files commonly use mode 644. Keep the site’s existing ownership policy; do not change ownership of the whole website to the web-server user.
Test and verify
Request a deliberately missing URL and check that the custom page appears with status 404. Test server errors in staging using a controlled test endpoint. Do not break a live configuration to create a 500 error.
Troubleshooting tips
Check the active virtual host, file paths, access permissions and error logs. An application may handle the error itself, or Nginx may be forwarding its response unchanged.