深度阅读

How to set up a custom error page in nginx?

作者
作者
2023年08月22日
更新时间
10.6 分钟
阅读时间
0
阅读量

To set up a custom error page in nginx, you can use the error_page directive in your nginx configuration file. Here is an example for setting up a custom 404 error page:

error_page 404 /404.html;
location = /404.html {
    internal;
    # path to your custom 404 error page
    root /path/to/your/website/;
}

In this example, error_page 404 /404.html specifies that nginx should serve the custom 404 error page located at /404.html whenever a 404 error occurs. The location block that follows sets the root directory for the custom 404 error page.

You can customize this example for other types of errors too, such as 500, 403, etc. For example, to set up a custom 500 error page, you can use:

error_page 500 /500.html;
location = /500.html {
    internal;
    # path to your custom 500 error page
    root /path/to/your/website/;
}

Note that the internal directive in the location block ensures that the custom error page is not accessible by clients directly, and can only be returned by nginx as an internal redirect.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。