NGINX: Create Custom 404 / 403 Error Pages on Linux or Unix

You can create custom 404 and 403 error pages in NGINX on Linux or Unix by following these steps:

  1. Create the error pages that you want to use. You can create these as HTML files and save them in a location that is accessible to NGINX.
  2. Open the NGINX configuration file:
sudo nano /etc/nginx/nginx.conf
  1. In the configuration file, find the server block for the website for which you want to create custom error pages. If you only have one website configured in NGINX, there will only be one server block.
  2. Within the server block, add the following lines to specify the custom error pages:
error_page 404 /404.html;
error_page 403 /403.html;
location = /404.html {
root /usr
/share/nginx/html;
internal;
}
location = /
403.html {
root /usr/share/nginx/html;
internal;
}

Note: Replace /usr/share/nginx/html with the location where you saved your custom error pages, and replace 404.html and 403.html with the names of your custom error pages.

  1. Save the changes to the configuration file and close it.
  2. Test the NGINX configuration to ensure that it is correct:
sudo nginx -t
  1. If the configuration is correct, restart NGINX to apply the changes:
sudo systemctl restart nginx

Now, when a user visits a URL that results in a 404 or 403 error, they will see your custom error pages instead of the default error pages.

Leave a Comment