In this tutorial, we will demonstrate how to OpenResty Edge Configure a custom error page in the web console of.

Custom Template Creation

Today, I'll show you how to OpenResty Edge Create error pages using custom templates in. Let's enter the console first. This is an example deployment of our console. Each user will have their own deployment.

Screenshot

First, we click "Global Config".

Screenshot

Then, we enter the "Global Page Templates" page. Here, we can create some global page templates, which can customize error pages or WAF block pages.

Screenshot

Now, we can click this button to add a simple template.

Screenshot

Name the template here as error-page-template-demo

Screenshot

For HTML content, you can use some predefined variables to customize it:

Screenshot

For example, "CLIENT_IP",

Screenshot

“CLIENT_COUNTRY”,

Screenshot

And "HOST".

Screenshot

Now we can output the client IP address.

Screenshot

You can also output the request ID and user agent as examples.

Screenshot

Then we click Save.

Screenshot

Since this is a global configuration, changes to the page template will be published and synchronized to all gateway servers immediately.

Screenshot

Application of template

Next, go to our example application:

Screenshot

Then search test-edge.com and apply the page template as a customized error page.

Screenshot

Enter the application.

Screenshot

Then enter the "Page Rules" page.

Screenshot

Click "New Rules".

Screenshot

Let's add a new action.

Screenshot

You can select or search action types here.

Screenshot

Search for "customize error page".

Screenshot

Here, you can select the HTTP response status code corresponding to the error page.

Screenshot

Here we only select "404".

Screenshot

There are several ways to set a custom error page, such as using the original HTML, the Backend URL, or the previously uploaded global static file.

Screenshot

Now, choose to use the global page template we just defined and named "error page template demo".

Screenshot

Next, we need to add another action.

Screenshot

Click the search box.

Screenshot

Search for "intercept upstream errors".

Screenshot

Since we have configured the proxy to the upstream, enabling this operation can intercept upstream errors.

Screenshot

In addition, we need to put this rule before other rules, so that we can return to our customized error page.

Screenshot

Save it.

Screenshot

Usually, we need to publish a new version of the "new page rule".

Screenshot

Click here to publish the pending changes.

Screenshot

Click Publish!

Screenshot

It is now fully synchronized.

Screenshot

The new page rules have been pushed to all gateway clusters and servers.

 Embeded video

Our configuration changes do not require a reload, restart, or binary upgrade of the server, so they are very efficient and easy to scale.

 Embeded image

Verification of page rules

Let's verify this rule by testing a gateway server.

Screenshot

Copy the IP address of this San Francisco gateway server.

Screenshot

Then test on the terminal.

Screenshot

This is the IP address of the San Francisco gateway server we saw earlier.

Screenshot

This is a randomly generated URL that does not exist, so it can be seen that this is indeed a 404 response.

Screenshot

Continue to run the command.

Screenshot

We can output the request information in HTML format, which is very helpful for troubleshooting the causes of error pages.

Screenshot

If you want to output more information or change the style of the error page, you just need to edit the page template. The changes will be released and take effect immediately.

Screenshot

Global override rules for error pages

If you want to use the same error page for all applications, you can also configure a global override rule.

Screenshot

Go to the "Global Rewrite Rules" page.

Screenshot

Click here to add a new rule, which is almost identical to the "Page Rules" in the application. We will introduce this function through another tutorial.

Screenshot

This is what I want to share today.

About the author

Zhang Yichun is open source OpenResty ® Project founder and OpenResty Inc. CEO and founder of the company.

Zhang Yichun (Github ID: agentzh) was born in Jiangsu, China, and now lives in the U.S. Bay Area. He was an advocate and leader of China's early open source technology and culture, and once worked for many internationally renowned high-tech enterprises, such as Cloudflare Yahoo, Alibaba, the pioneer of "edge computing", "dynamic tracking" and "machine programming", has more than 22 years of programming and 16 years of open source experience. As the leader of open source projects with more than 40 million global domain name users, he OpenResty ® High tech enterprises created by open source projects OpenResty Inc. It is located in the center of Silicon Valley in the United States. Its two main products OpenResty XRay (Utilize Dynamic tracking Technology) and OpenResty Edge (The all-purpose gateway software most suitable for microservices and distributed traffic), widely favored by many listed and large enterprises worldwide. Besides OpenResty, Zhang Yichun has contributed more than one million lines of code to many open source projects, including Linux kernel, Nginx LuaJIT GDB SystemTap LLVM , Perl, etc., and has written more than 60 open source software libraries.

Follow us

If you like this article, please follow us OpenResty Inc. Corporate Blog Site You are also welcome to scan our WeChat official account:

Our

translate

We provide English version Original text and Chinese translation (this article). We also welcome readers to provide translations in other languages. As long as the full text translation is not omitted, we will consider using it. Thank you very much!