Nginx is a high performance web server capable of serving content with flexibility and power. When designing your web pages, it is often helpful to customize every piece of content that your users will see. This includes error pages for when they request content that is not available. In this guide, we’ll demonstrate how to configure Nginx to use custom error pages on CentOS 7.
To get started on with this guide, you will need a non-root user with sudo
privileges. You can set up a user of this type by following along with our initial set up guide for CentOS 7. You will also need to have Nginx installed on your system. Learn how to set this up by following this guide.
We will create a few custom error pages for demonstration purposes, but your custom pages will obviously be different.
We will put our custom error pages in the /usr/share/nginx/html
directory where CentOS’s Nginx sets its default document root. We’ll make a page for 404 errors called custom_404.html
and one for general 500-level errors called custom_50x.html
. You can use the following lines if you are just testing. Otherwise, put your own content in these locations:
- echo "<h1 style='color:red'>Error 404: Not found :-(</h1>" | sudo tee /usr/share/nginx/html/custom_404.html
- echo "<p>I have no idea where that file is, sorry. Are you sure you typed in the correct URL?</p>" | sudo tee -a /usr/share/nginx/html/custom_404.html
- echo "<h1>Oops! Something went wrong...</h1>" | sudo tee /usr/share/nginx/html/custom_50x.html
- echo "<p>We seem to be having some technical difficulties. Hang tight.</p>" | sudo tee -a /usr/share/nginx/html/custom_50x.html
We now have two custom error pages that we can serve when client requests result in different errors.
Now, we just need to tell Nginx that it should be utilizing these pages whenever the correct error conditions occur. We’ll need to adjust our server blocks. On CentOS 7, the primary server block is located in the /etc/nginx/nginx.conf
file. We’ll be configuring this server block, but you should adjust whatever other server blocks you have configured:
- sudo nano /etc/nginx/nginx.conf
Inside the file, locate the block that defines the server context. We can now point Nginx to our custom error pages.
The CentOS Nginx configuration file already defines a 404 error page using the error_page
directive. We’ll need to change this so that when a 404 error occurs (when a requested file is not found), the custom page you created is served. We’ll adjust the associated location block for the file so 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):
http {
. . .
server {
. . .
error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
. . .
}
}
Usually, we would not have to set the root
in the new location block since it matches the root in the server block. However, we are being explicit here so that our error pages are served even if we move our regular web content and the associated document root to a different location.
Next, we can add the directives to make sure that when Nginx encounters 500-level errors (server-related problems), it will serve the other custom page we made. This will follow the exact same formula we used in the last section. This time we set multiple 500-level errors to all use the custom_50x.html
page:
http {
. . .
server {
. . .
error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
error_page 500 502 503 504 /custom_50x.html;
location = /custom_50x.html {
root /usr/share/nginx/html;
internal;
}
location /testing {
fastcgi_pass unix:/does/not/exist;
}
}
}
At the bottom, we also added a dummy FastCGI pass so that we can test our 500-level error page. This will not work correctly since the backend does not exist. Requesting a page here will allow us to test that 500-level errors serve our custom page.
Save and close the file when you are finished.
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 systemctl restart nginx
Now, when you go to your server’s domain or IP address and request a non-existent file, you should see the 404 page we set up:
http://server_domain_or_IP/thiswillerror
When you go to the location we set up for the FastCGI pass, we will receive a 502 Bad Gateway error with our custom 500-level page:
http://server_domain_or_IP/testing
You can now go back and remove the fake FastCGI pass location from your Nginx config.
You should now be serving custom error pages for your site. This is an easy way to personalize your users’ experience even when they are experiencing problems. One suggestion for these pages is to include links to locations where they can go to get help or more information. If you do this, make sure that the link destinations are accessible even when the associated errors are occurring.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!