Nginx · December 18, 2023

Nginx for Newbie: setting up error_page directive

Nginx for Newbie: Setting up error_page directive

When it comes to web servers, Nginx has gained immense popularity due to its high performance, scalability, and flexibility. Whether you are a seasoned developer or a newbie, understanding how to configure Nginx properly is crucial for a smooth web hosting experience. In this article, we will focus on one important aspect of Nginx configuration - the error_page directive.

What is the error_page directive?

The error_page directive in Nginx allows you to customize the error pages that are displayed to users when they encounter certain HTTP errors. By default, Nginx provides generic error pages for common HTTP errors like 404 Not Found or 500 Internal Server Error. However, you can use the error_page directive to create custom error pages that align with your website's design and branding.

Setting up the error_page directive

To set up the error_page directive in Nginx, you need to modify your Nginx configuration file. The configuration file is usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf. Here's how you can proceed:

  1. Open the Nginx configuration file using a text editor of your choice.
  2. Locate the server block where you want to configure the error_page directive. This block typically contains the server_name directive.
  3. Add the following line within the server block:
error_page 404 /404.html;

In the above example, we are specifying that when a 404 error occurs, Nginx should display the 404.html file located in the root directory of the website.

You can also specify an absolute path to the error page file:

error_page 500 /var/www/html/errors/500.html;

In this case, Nginx will display the 500.html file located at /var/www/html/errors/ when a 500 error occurs.

Using named locations with error_page

The error_page directive also allows you to use named locations, which can be useful for more complex error handling scenarios. Here's an example:

location / {
    error_page 404 = @fallback;
}

location @fallback {
    proxy_pass http://backend;
}

In the above example, when a 404 error occurs, Nginx will redirect the request to the named location @fallback, which in turn proxies the request to the backend server specified by proxy_pass.

Testing the error_page configuration

After making changes to your Nginx configuration file, it's important to test the configuration to ensure there are no syntax errors. You can do this by running the following command:

nginx -t

If the configuration is valid, you can reload Nginx to apply the changes:

nginx -s reload

Summary

The error_page directive in Nginx is a powerful tool for customizing error pages and enhancing the user experience on your website. By following the steps outlined in this article, you can easily set up the error_page directive in your Nginx configuration file. Remember to test your configuration and reload Nginx to apply the changes.

For reliable and high-performance VPS hosting solutions, consider Server.HK. Our Hong Kong VPS hosting plans offer the scalability and flexibility you need to power your websites and applications.