Global Rewrite Rule

For different applications, many Page Rules are the same. These rules can be configured by using Global Custom Action, and also Global Rewrite Rule.

Global Rewrite Rule is different from Global Custom Action. To work, the latter must be added to the application and released.

But the former is not sensitive to the application, and does not need to be released. Just add a Rewrite Rules, and it will directly work on all applications.

So use Global Rewrite Rule with great caution.

First, we create an application without any Rewrite Rules:

Then, directly access without adding Rewrite Rules:


$ curl -i -H 'host: rewrite-rule-test.com' 52.43.45.19/404.html

HTTP/1.1 404 Not Found

Server: openresty+

Date: Mon, 21 Jan 2019 15:59:57 GMT

Content-Type: text/html

Content-Length: 166

Connection: keep-alive

<html>

<head><title>404 Not Found</title></head>

<body bgcolor="white">

<center><h1>404 Not Found</h1></center>

<hr><center>openresty</center>

</body>

</html>

As expected, 404 error information is returned.

Then, go to the Global Rewrite Rule page in Global Config to newly add a rule.

The Global Rewrite Rule page has listed all existing global rules.

Click the New Rules button to create a new one.

Similar to Rewrite Rules in the application, this page contains two parts, i.e. Enable Conditions and Action.

Below is a simple example of unconditionally returning a character string.

The Rewrite Rules drop-down list includes a variety of actions. From the “Response Action” category among them, select Output Response Body.

Click the Create button on the bottom right-hand corner, and a Global Rewrite Rule is set successfully.

This Global Rewrite Rule, without going through the release, already works on the above added application.

Access to see what value will be returned:


$ curl -i -H 'host: rewrite-rule-test.com' 52.43.45.19/404.html
HTTP/1.1 200 OK
Date: Tue, 14 Nov 2023 10:51:34 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Server: OpenResty Edge
Req-ID: 00002a000e80584eb9b0b942

<!doctype html>
<html>
<head>
  <title>Response</title>
</head>
<body>
</body>
</html>

The response body set in the Global Rewrite Rule is returned, rather than the 404 error information.

Now, try by deleting the Global Rewrite Rule.

Access again and 404 is returned again:


$ curl -i -H 'host: rewrite-rule-test.com' 52.43.45.19/404.html

HTTP/1.1 404 Not Found

Server: openresty+

Date: Mon, 21 Jan 2019 16:00:11 GMT

Content-Type: text/html

Content-Length: 166

Connection: keep-alive

<html>

<head><title>404 Not Found</title></head>

<body bgcolor="white">

<center><h1>404 Not Found</h1></center>

<hr><center>openresty</center>

</body>

</html>