Nginx error page configuration to handle website failures gracefully

WBOY
Release: 2023-07-04 16:06:10
Original
2282 people have browsed it

Nginx error page configuration, elegant handling of website failures

In the modern Internet era, a highly stable and reliable website is the goal pursued by any business or individual. However, for a variety of reasons, the website may experience glitches or errors, which may be due to network issues, server issues, or application errors, among others.

In order to provide a better user experience and handle any errors that may occur gracefully, Nginx, as a powerful web server software, can not only provide high-performance services, but also flexibly configure error pages.

In Nginx, error page configuration is very simple. By editing Nginx's configuration file, you can set up a custom error page and associate it with the corresponding error code.

First, you need to open the Nginx configuration file, which can be /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf, the specific file path may be different, depending on your installation method.

Find the http section in the configuration file, and then add the following code within the section:

http {
    # ...

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /40x.html {
        root /usr/share/nginx/html;
    }

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # ...
}
Copy after login

In the above code, we set up two error pages: 404 pages and 50x pages. The 404 page is the page displayed when the accessed resource does not exist, and the 50x page is the page displayed when the server returns a 500, 502, 503 or 504 error.

Next, we need to create these error pages. By default, Nginx error pages should be located in the /usr/share/nginx/html/ directory. Create two HTML files 404.html and 50x.html in this directory and fill in the customized content.

404.html Sample code:

<!DOCTYPE html>
<html>
<head>
    <title>404 - Not Found</title>
</head>
<body>
    <h1>404 - Not Found</h1>
    <p>对不起,你访问的页面不存在。</p>
</body>
</html>
Copy after login

50x.html Sample code:

<!DOCTYPE html>
<html>
<head>
    <title>50x - Server Error</title>
</head>
<body>
    <h1>50x - Server Error</h1>
    <p>对不起,服务器出现问题,请稍后再试。</p>
</body>
</html>
Copy after login

After saving and closing the configuration file, reload the Nginx configuration:

sudo service nginx reload
Copy after login

Now, if a 404 error page or server error page appears on your website, Nginx will automatically display your customized error page instead of the default Nginx error page.

In addition to setting a static HTML page as an error page, you can also use a dynamic script language to generate an error page. For example, you can use PHP, Python, or any other supported language to generate a custom error page.

The sample code is as follows:

error_page 404 /404.php;
error_page 500 502 503 504 /50x.php;

location = /40x.php {
    root /usr/share/nginx/html;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
}

location = /50x.php {
    root /usr/share/nginx/html;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
}
Copy after login

Please ensure that the corresponding script language interpreter and FastCGI service have been installed and configured on your server.

In this way, you can generate more interactive and personalized error pages based on actual needs, providing a better user experience.

In summary, configuring Nginx error pages is a simple yet powerful technique that can help us handle website failures gracefully. Whether it is a 404 page or a server error page, by customizing error pages, we can provide a better user experience and convey useful information to users. At the same time, we can also use scripting language to generate dynamic error pages to meet more advanced needs.

I hope this article will help you understand and configure Nginx error pages!

The above is the detailed content of Nginx error page configuration to handle website failures gracefully. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!