How to create Nginx custom error pages

It is always better to show the helpful information to the users instead of the default raw message. In this guide, we’ll demonstrate how to configure Nginx to use custom error pages.

Let’s create a directory called errors in the root folder. Inside error folder, create a file called error_404.html for 404 error messages and create a file called error_50x.htlm for 500-level error messages.

error_404.html

error_50x.html

Now open your nginx config file, generally, it will be located at /etc/nginx/sites-available/

Open config file with vi editor (you can use other editor also)

We use error_page directive to serve custom pages which you have created, as shown below we will use error_page directive with location block.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/visitors

Add this to consutome 404 error page –

Add this to serve custom 50x-level error page –

Here is the complete config file (/etc/nginx/sites-available/).

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status