Request/Response Header Manipulation

更新时间:
复制 MD 格式

You can add or remove request and response headers before requests are forwarded. A virtual service lets you modify headers without changing your application code. This topic describes how to configure request and response header manipulation.

Prerequisites

Make sure that you have completed the preparations and deployed the httpbin and sleep services. For more information, see Preparations.

Procedure

  1. Verify the service configuration.

    1. In the KubeConfig environment for your ACK cluster, run the following command to enter the sleep environment and start a bash session.

      kubectl exec -it deploy/sleep -- sh
    2. Run the following command to send a request to the httpbin service.

      curl -I httpbin:8000

      Expected output:

      HTTP/1.1 200 OK
      server: envoy
      date: Fri, 11 Aug 2023 09:50:24 GMT
      content-type: text/html; charset=utf-8
      content-length: 9593
      access-control-allow-origin: *
      access-control-allow-credentials: true
      x-envoy-upstream-service-time: 3
  2. Use the following content to configure traffic rules for the httpbin application and add a header modification configuration. For more information, see Manage virtual services.

  3. apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: httpbin-vs
      namespace: default
    spec:
      hosts:
        - httpbin
      http:
        - route:
            - destination:
                host: httpbin
              headers:
                response:
                  add:
                    httpbin: hello
                  remove:
                    - x-envoy-upstream-service-time
  4. Verify the configuration.

    1. In the KubeConfig environment for your ACK cluster, run the following command to enter the sleep environment and start a bash session.

      kubectl exec -it deploy/sleep -- sh
    2. Run the following command to send a request to the httpbin service.

      curl -I httpbin:8000

      Expected output:

      HTTP/1.1 200 OK
      server: envoy
      date: Fri, 11 Aug 2023 09:49:37 GMT
      content-type: text/html; charset=utf-8
      content-length: 9593
      access-control-allow-origin: *
      access-control-allow-credentials: true
      httpbin: hello

      The output shows that the header configuration in the virtual service has taken effect. The x-envoy-upstream-service-time header is removed, and the httpbin: hello header is added.