All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure a path rewriting rule

Last Updated:Oct 12, 2023

After you configure a path rewriting rule, the path of an HTTP request can be rewritten to a specified value before being forwarded to the destination application. This topic describes how to configure a path rewriting rule.

Prerequisites

The preparations are completed, and the HTTPBin and sleep services are deployed. For more information, see Preparations.

Procedure

  1. Configure a virtual service for the HTTPBin service to rewrite the path to /status/400 if the path in a request destined for the HTTPBin service is /get.

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Traffic Management Center > VirtualService. On the page that appears, click Create.

    3. On the Create page, configure the following parameters, and click Preview. Confirm that the YAML content is correct, click Submit, and then click Create.

      【Sidecar】Path Rewrite 重写请求路径.png

      The YAML preview is as follows.

      apiVersion: networking.istio.io/v1beta1
      kind: VirtualService
      metadata:
        name: httpbin
        namespace: default
      spec:
        hosts:
          - httpbin.default.svc.cluster.local
        http:
          - rewrite:
              uri: /status/400
            route:
              - destination:
                  host: httpbin.default.svc.cluster.local
  2. Use kubectl to connect to the Container Service for Kubernetes (ACK) cluster based on the information in the kubeconfig file, and then run the following command to check whether the path rewriting configuration takes effect:

    kubectl exec -it deploy/sleep -- curl httpbin:8000/get -I

    Expected output:

    HTTP/1.1 400 Bad Request
    server: envoy
    date: xxx, xx xxx 202x xx:xx:xx GMT
    content-type: text/html; charset=utf-8
    access-control-allow-origin: *
    access-control-allow-credentials: true
    content-length: 0
    x-envoy-upstream-service-time: 4

    You can see that a request is sent to the /get path of the HTTPBin service in the sleep container. Requests sent to this path should be responded with the status code 200. However, in this case, the status code 400 is returned. The output indicates that the path rewriting configuration takes effect and the path of the request is rewritten to /status/400.