Setting Up a Custom 404 Page on Nginx
A 404 page is a standard response code in HTTP telling the user, in effect, that they've clicked on a broken link. Setting up a custom 404 page on Nginx is a simple and straightforward process.
Create Your 404 Page
Design a custom 404.html
page and save it in your website's root directory.
For example /var/www/your-site/public_html/404.html
.
Edit Nginx Configuration
Open your Nginx configuration file, commonly found at one of the following locations:
/etc/nginx/nginx.conf
/etc/nginx/sites-available/your-site.conf
.
Open file with your favorite text editor and add the following lines:
server {
...
404 /404.html;
location = /404.html {
/var/www/your-site/public_html;
internal;
}
...
}
Replace /var/www/your-site/public_html
with your website's root directory.
Test Configuration
Be careful when editing your Nginx configuration file. A single typo can cause your website to go down.
Run sudo nginx -t
to check for syntax errors in your configuration.
Reload Nginx
If the test is successful, reload Nginx with sudo service nginx reload
.
Done!
Your custom 404 page is now set up on Nginx. Test it with a non-existent URL.