Nginx Error Pages: How to Show a Custom 404 and 502 Page
When a visitor hits a missing page or your backend goes down, Nginx shows a plain white page with a short error line by default. That default page tells the visitor nothing useful and makes a small site look abandoned. Setting up nginx custom error pages takes about ten minutes, and it turns those dead ends into pages that point people back to working content. This guide walks through the error_page directive, named locations, and the extra step needed when Nginx sits in front of PHP-FPM or another backend.
You need a server with Nginx already installed and a site configured in a server block. The examples below use Ubuntu 24.04 paths, but the directives work the same on any Linux install. If you have not set up a site yet, start with our guide on how to set up Nginx server blocks on Ubuntu 24.04 and come back once the site loads.
How Nginx Picks an Error Page
The error_page directive decides which page Nginx shows for a given status code. You list one or more codes, then point Nginx at a URI, which can be a static file or a named location.
error_page 404 /404.html;
error_page 502 503 504 /50x.html;
Two details catch people out. First, the URI after the code is an internal redirect, not a file path, so /404.html has to exist under the site root or inside a location block that can serve it. Second, the directive inherits from the http and server levels down into locations, but a location can override it.
Build the 404 and 502 Pages
Keep the pages static HTML. A static file has no dependency on PHP, a database, or an upstream server, so it still loads when the thing that broke is the backend itself. That is the whole point of a 502 page: the backend is down, so the page must not need it.
Create the files in your site root, next to index.html or index.php. A 404 page should say the page is gone and link to the homepage, plus a search box or a short list of popular pages. A 502 page should say the site is temporarily unavailable and ask the visitor to try again in a few minutes. Keep both pages small and avoid external images, fonts, or scripts that could fail at the same time.
<!-- /var/www/example.com/404.html -->
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Page not found</title></head>
<body>
<h1>This page does not exist</h1>
<p>The link may be old, or the address may have a typo.</p>
<p><a href="/">Go to the homepage</a></p>
</body>
</html>
Check the file permissions after you create the pages. Nginx runs as www-data on Ubuntu, so the files need to be readable by that user, or visitors get a 403 on the error page itself.
Wire the Pages Into a Server Block
With the files in place, add the directives to your site config. The typical pattern puts the error pages in a named location marked internal, which stops visitors from opening /404.html directly as a normal page.
server {
listen 80;
server_name example.com;
root /var/www/example.com;
error_page 404 /404.html;
error_page 502 503 504 /50x.html;
location = /404.html {
internal;
}
location = /50x.html {
internal;
}
location / {
try_files $uri $uri/ =404;
}
}
The internal flag matters. Without it, someone can request example.com/404.html and get a 200 response for a page that claims something is missing, which search engines can index as a real page. With the flag, a direct request returns 404, which is the correct behaviour. Test the config and reload Nginx after every edit:
sudo nginx -t
sudo systemctl reload nginx
If the site is WordPress, a missing page can come from Nginx or from WordPress itself, and the two look different. Our guide on how to fix Nginx 404 errors on a fresh install walks through telling the two apart before you change any config.
Use a Named Location for More Control
Named locations start with @ and exist only for internal redirects. They are handy when the error page needs logic that a static file cannot provide, for example serving a different page per site on a shared server, or falling back to a second file if the first is missing.
error_page 404 @error404;
error_page 502 503 504 @error50x;
location @error404 {
root /var/www/shared/errors;
try_files /404.html =404;
}
location @error50x {
root /var/www/shared/errors;
try_files /50x.html =503;
}
This pattern keeps one copy of the error pages in a shared folder and points every site at it, so a design change means editing one file instead of ten. The try_files fallback protects you if the shared file is deleted by accident: Nginx returns the plain status code instead of looping.
Make 502 Pages Work Behind a Reverse Proxy
By default, Nginx passes error responses from an upstream server straight through to the visitor. If PHP-FPM crashes, the visitor sees the upstream 502 response, not your custom page. To make Nginx replace upstream errors with its own pages, add proxy_intercept_errors on in the location that proxies to the backend.
location / {
proxy_pass http://127.0.0.1:8080;
proxy_intercept_errors on;
error_page 502 503 504 /50x.html;
}
The same idea applies to FastCGI. If PHP-FPM returns a 502, Nginx only shows your page when fastcgi_intercept_errors on is set in the PHP location block. Forgetting this one line is the most common reason a custom 502 page never appears on a LEMP stack, even though the 404 page works fine.
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_intercept_errors on;
}
If your 502 errors keep coming back, the page is only half the job. Our guide on fixing 502 Bad Gateway errors in Nginx covers the usual causes, from a stopped PHP-FPM pool to a wrong socket path, so you can fix the backend instead of only dressing up the error.
Set Different Pages Per Site
On a server with several sites, each server block can declare its own error_page lines and give each site its own branding. You can also set a server-wide default in the http block of nginx.conf and let individual sites override it.
# /etc/nginx/nginx.conf
http {
error_page 404 /errors/404.html;
error_page 502 503 504 /errors/50x.html;
# ... other settings
}
For the global default to work, the /errors/ files need a location that serves them from a folder every site can read. Keep the global pages generic, with no site name or logo, since any site on the server can fall back to them.
Watch out for location priority
Error page URIs go through the same location matching as any other request. A broad regex location can swallow /404.html before the exact-match location gets it, and then the error page itself returns the wrong response. If your error page loads as a download, a PHP error, or a different page entirely, check your location order first. The rules are covered in our guide to how Nginx location block priority works.
Test Your Nginx Custom Error Pages With Curl
Never trust a browser test alone. Browsers cache error pages, and some show their own built-in error page for short responses, which hides what Nginx actually sent. Curl shows the real status code and body.
# 404: request a path that does not exist
curl -i https://example.com/this-page-does-not-exist
# 502: stop PHP-FPM, request a PHP page, then start it again
sudo systemctl stop php8.3-fpm
curl -i https://example.com/
sudo systemctl start php8.3-fpm
Check three things in the output. The status line should read 404 or 502, not 200. The body should be your page, not the Nginx default. If the status line says 200, your error page location is missing the internal flag or a rewrite is turning the error into a normal page. If you still see the default Nginx page, the error_page line is in the wrong block or the config was not reloaded.
For ongoing checks, log monitoring helps. Our guide on how to monitor Nginx error logs and get alerts on 5xx spikes shows how to watch for 502 errors in the log and get notified before visitors start reporting them.
Keep the Pages Working When the Backend Is Down
A custom error page is only useful if it survives the outage it announces. Keep the pages static and self-contained, with inline CSS and no external assets. Test them after every Nginx change, not just once at setup, because a new location block can break the error page route without touching the error page itself. And check the error log after a real outage to confirm which page Nginx served.
Put the test into your deploy checklist: request a missing URL, confirm the 404 page, stop the backend for a minute, confirm the 502 page, then start it again. That two-minute check catches the intercept flag you forgot and the permission problem on the new file. Once it passes, your nginx custom error pages are ready for the next real outage.