All Products
Search
Document Center

Microservices Engine:Configure a rewrite policy

Last Updated:Jan 10, 2024

You can configure a rewrite policy to change the paths and hostnames in requests before you forward the requests to their destination backend services. This meets the requirements for specific business environments and architectures. The rewrite policy provides precise control over the paths and hostnames in requests and ensures that the requests are correctly routed to the appropriate service or endpoint.

Rewrite policies

A rewrite policy allows you to change the paths and hostnames in requests.

Path rewrite

Cloud-native gateways allow you to rewrite the paths in requests by using one of the following methods: exact rewrite, prefix rewrite, and regex rewrite.

Exact rewrite

Completely changes the paths in requests.

Example 1

If the path in a request is /app/test and you want the path to be rewritten as /foo/bar, we recommend that you configure the following rewrite policy:

  • Route matching condition: Set the matching type of the route to Exact Match, and set the path to /app/test.

  • Rewrite policy: Set the rewrite method to Exact Rewrite, and set the path to /foo/bar.

Important

If you want to use the exact rewrite method, the matching type of the route must be Exact Match or Regex Match. The matching type of the route cannot be Prefix Match.

Prefix rewrite

Changes the path prefix in a request.

Example 1

If the path in a request is /app/test and you want the path to be rewritten as /test, we recommend that you configure the following rewrite policy:

  • Route matching condition: Set the matching type of the route to Prefix Match, and set the path to /app/.

  • Rewrite policy: Set the rewrite method to Prefix Rewrite, and set the path to /.

Note

The forward slash (/) at the end of /app/ cannot be omitted. This is because only the matched prefix is changed if the prefix rewrite method is used. If you set the path in the matching condition to /app, the path is rewritten as //test, which is an invalid path.

Example 2

If the path in a request is /v1/test and you want the path to be rewritten as /v2/test, we recommend that you configure the following rewrite policy:

  • Route matching condition: Set the matching type of the route to Prefix Match, and set the path to /v1.

  • Rewrite policy: Set the rewrite type of the rewrite policy to Prefix Rewrite, and set the path to /v2.

Important

If you want to use the prefix rewrite method, the matching type of the route must be Prefix Match. The matching type of the route cannot be Exact Match or Regex Match. If the matching type of a route is set to Prefix Match, all requests with paths that contain the specified prefix are matched and rewritten. Before you set the rewrite type of the rewrite policy to Prefix Rewrite, you must confirm that all requests need to be rewritten. Otherwise, we recommend that you set the rewrite type of the route to Exact Rewrite.

Regex rewrite

Partially changes the path in a request. A regex rewrite configuration consists of the Mode and Replacement parameters. The value of the Mode parameter is used to match the specified parts that you want to modify in the original path. The value of the Replacement parameter is used to replace the matched parts. For information about the regular expression syntax, see RE2 Regular expression syntax.

Example 1

If the path in a request is /aaa/one/bbb/one/ccc and you want the path to be rewritten as /aaa/two/bbb/two/ccc, we recommend that you configure the following rewrite policy:

  • Route matching condition: Set the matching type of the route to Exact Match, and set the path to /aaa/one/bbb/one/ccc.

  • Rewrite policy: Set the rewrite type to Regex Rewrite, set the Mode parameter to one, and set the Replacement parameter to two.

Example 2

If the path in a request is /httpbin/(.*)/(.*) and you want to remove the prefix /httpbin from the path and exchange the positions of the two regex parts, we recommend that you configure the following rewrite policy:

  • Route matching condition: Set the matching type of the route to Regex Match, and set the path to /httpbin/(.*)/(.*).

  • Rewrite policy: Set the rewrite type to Regex Rewrite, set the Mode parameter to /httpbin/(.*)/(.*), and set the Replacement parameter to /\2/\1. \1 specifies the first string that is matched by using the specified regular expression. \2 specifies the second string that is matched by using the specified regular expression. The \1 and \2 strings correspond to $1 and $2 in NGINX Ingress.

Note

The regex rewrite method is more complex than the prefix rewrite and exact rewrite methods. We recommend that you use the exact rewrite method.

Host rewrite

Cloud-native gateways allow you to changes the hostnames in requests by using the exact rewrite method.

For example, if the hostname in a request is test.com, and you want the hostname to be rewritten as dev.com, you must set Destination Host in the rewrite policy to dev.com.

Configure a rewrite policy

  1. Log on to the MSE console.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. In the top navigation bar, select a region.

  3. On the Gateways page, click the name of the gateway.

  4. In the left-side navigation pane, choose Routes > Route Settings. On the page that appears, find the routing rule that you want to modify and click Policies in the Actions column.

  5. In the Policy Settings section of the page that appears, click the Rewrite tab. Then, click the 编辑图案 icon on the right of the rewrite policy.

  6. Configure the rewrite policy in the Rewrite section and click OK.

  7. Note
    • If Type in the Original Path parameter is Exact Match or Regex Match, you can set Type in the Destination Path parameter to Exact Rewrite.

    • If Type in the Original Path parameter is Prefix Match, you can set Type in the Destination Path parameter to Prefix Rewrite or Regex Rewrite.

    • If Type in the Original Path parameter is Regex Match, you can set Type in the Destination Path parameter to Exact Rewrite or Regex Rewrite.

  8. After the rewrite policy is configured, turn on the Enabled switch.

    • If you enable the rewrite policy, the paths and hostnames in requests are changed based on the rewrite policy when the gateway forwards the requests to their destination backend services.

    • If you do not enable the rewrite policy, the paths and hostnames in requests are not changed.

Verify the result

In this example, the /app1 prefix is used to identify external requests, but the paths with this prefix cannot be processed by the backend service. You can use the prefix rewrite policy to rewrite /app1/ as / to ensure that the requests can be forwarded.

  • Sample URL with the original path:

    curl -I http://121.196.XX.XX/demo/item/list
  • Sample URL after a path rewrite: (The destination path after the rewrite is still /demo/item/list.)

    curl -I http://121.196.XX.XX/app1/demo/item/list

References

For more information about redirection, see Redirection.