• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx for Newbie: setting up error_page directive

December 18, 2023

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.

Recent Posts

  • How to Migrate Your Website to a Hong Kong VPS: Zero-Downtime Transfer Guide (2026)
  • How to Set Up Redis on Hong Kong VPS: Caching, Queues, and Session Storage (2026)
  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot