Configuring Nginx to Use your Error Pages
Now, we just need to tell Nginx that it should be utilizing these pages whenever the correct error conditions occur. Open the server block file in the /etc/nginx/sites-enabled directory that you wish to configure. We will use the default server block file called default, but you should adjust your own server blocks if you’re using a non-default file:
sudo nano /etc/nginx/sites-enabled/default
Direct 404 Errors to the Custom 404 Page
Use the error_page directive so that when a 404 error occurs (when a requested file is not found), the custom page you created is served. We will create a location block for the file, where we are able to ensure that the root matches our file system location and that the file is only accessible through internal Nginx redirects (not requestable directly by clients):
/etc/nginx/sites-enabled/default
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; . . . error_page 404 /custom_404.html; location = /custom_404.html { root /usr/share/nginx/html; internal; } }
Restarting Nginx and Testing your Pages
Test your configuration file’s syntax by typing:
sudo nginx -t
If any errors were reported, fix them before continuing. When no syntax errors are returned, restart Nginx by typing:
sudo service nginx restart