Configure a rewrite policy

更新时间:
复制 MD 格式

A rewrite policy modifies a request's path and hostname before it is forwarded to a backend service. This provides precise control to ensure that requests are correctly routed, meeting specific business and architectural requirements.

Rewrite policies

A rewrite policy modifies the path and hostname of a request.

Path rewrite

For path rewrites, Cloud-native Gateway supports three rewrite modes: exact rewrite, prefix rewrite, and regex rewrite.

Exact rewrite

An exact rewrite completely replaces the original request path.

Example 1

To forward a request with the path /app/test to a backend service at /foo/bar, configure the policy as follows:

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

  • Rewrite: Set the rewrite type to exact rewrite and the path to /foo/bar.

Important

An exact rewrite is compatible only with the Exact Match or Regex Match route matching types. It cannot be used with Prefix Match.

Prefix rewrite

A prefix rewrite replaces the prefix of the original request path.

Example 1

To forward a request with the path /app/test to a backend service at /test, configure the policy as follows:

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

  • Rewrite: Set the rewrite type to prefix rewrite and the path to /.

Note

The path for the route matching condition must be set to /app/ because a prefix rewrite only modifies the matched prefix string. If the path for the route matching condition is set to /app, the rewrite results in an incorrect path of //test.

Example 2

To forward a request with the path /v1/test to a backend service at /v2/test, configure the policy as follows:

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

  • Rewrite: Set the rewrite type to prefix rewrite and the path to /v2.

Important

Prefix rewrite works only with the Prefix Match route matching type. Be aware that this rewrite applies to all requests that share the specified prefix. To rewrite a single, specific path, use exact rewrite instead.

Regex rewrite

A regex rewrite modifies parts of a request path. It requires a pattern to match the path segment to be modified and a replacement that will replace the matched segment. For information about the regular expression specification, see Regular Expression Syntax.

Example 1

To forward a request with the path /aaa/one/bbb/one/ccc to a backend service at /aaa/two/bbb/two/ccc, configure the policy as follows:

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

  • Rewrite: Set the rewrite type to regex rewrite, the pattern to one, and the replacement to two.

Example 2

To process a request path like /httpbin/(.*)/(.*) by removing the /httpbin prefix and swapping the two captured groups, configure the policy as follows:

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

  • Rewrite: Set the rewrite type to regex rewrite, the pattern to /httpbin/(.*)/(.*), and the replacement to /\2/\1. Here, \1 represents the string captured by the first group, and \2 represents the string captured by the second group. This is similar to the $1 and $2 syntax used in NGINX.

Note

Regex rewrite is an advanced feature with complex syntax, intended for special scenarios. Use exact rewrite whenever possible.

Hostname rewrite

For hostname rewrites, Cloud-native Gateway supports exact rewrite.

For example, to forward a request with the hostname test.com to a backend service with the hostname dev.com, set the destination hostname to dev.com in the rewrite policy.

Configure a rewrite policy

  1. Log on to the MSE console. In the top navigation bar, select a region.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the ID of the gateway.

  3. In the left-side navigation pane, click Routes. Then, click the Routes tab.

  4. Find the route rule that you want to modify. In the Actions column, click Policies.

  5. On the Policies tab, click Rewrite. Configure the route rule, and then click Save.

    Note
    • For a route using Exact Match or Regex Match, you can configure an exact rewrite.

    • For a route using Prefix Match, you can configure a prefix rewrite or regex rewrite.

    • For a route using Regex Match, you can configure an exact rewrite or regex rewrite.

  6. After the rewrite rule configuration is complete, turn on the Enable switch. In the Enable dialog box, click OK.

    • Enabled: The gateway applies the rewrite policy, modifying the request path and hostname before forwarding it to the backend service.

    • Disabled: The gateway forwards requests to the backend service without modifying their path or hostname.

Verify the results

For example, if your services are exposed with a common prefix like /app1/ that the backend service does not use, you can apply a Prefix Rewrite to change the /app1/ prefix to /. This ensures correct request forwarding.

  • The backend service listens on a specific path:

    curl -I http://121.196.XX.XX/demo/item/list
  • After the rewrite policy is enabled, you can send a request with the prefix /app1/ to the gateway, which forwards it to the correct backend path:

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

Related documents

For more information about routes, see Routes.