Host rewrites

Replace the host header value before forwarding a request to a backend service by using the URLRewrite filter.

For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Follow the Get started guide to install kgateway.

  2. Follow the Sample app guide to create an API gateway proxy with an HTTP listener and deploy the httpbin sample app.

  3. Get the external address of the gateway and save it in an environment variable.

    export INGRESS_GW_ADDRESS=$(kubectl get svc -n kgateway-system http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}")
    echo $INGRESS_GW_ADDRESS  
    kubectl port-forward deployment/http -n kgateway-system 8080:8080

Rewrite hosts

  1. Create an HTTPRoute resource for the httpbin app that uses the URLRewrite filter to rewrite the hostname of th request. In this example, all incoming requests on the rewrite.example domain are rewritten to the www.example.com host.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin-rewrite
      namespace: httpbin
    spec:
      parentRefs:
      - name: http
        namespace: kgateway-system
      hostnames:
        - rewrite.example
      rules:
         - filters:
           - type: URLRewrite
             urlRewrite:
               hostname: "www.example.com"
           backendRefs:
            - name: httpbin
              port: 8000
    EOF
    Setting Description
    spec.parentRefs The name and namespace of the Gateway that serves this HTTPRoute. In this example, you use the http gateway that was created as part of the get started guide.
    spec.rules.filters.type The type of filter that you want to apply to incoming requests. In this example, the URLRewrite filter is used.
    spec.rules.filters.urlRewrite.hostname The hostname that you want to rewrite requests to.
    spec.rules.backendRefs The backend destination you want to forward traffic to. In this example, all traffic is forwarded to the httpbin app that you set up as part of the get started guide.
  2. Send a request to the httpbin app on the rewrite.example domain. Verify that you get back a 200 HTTP response code and that you see the Host: www.example.com header in your response.

    ℹ️
    The following request returns a 200 HTTP response code, because you set up an HTTPRoute for the httpbin app on the www.example.com domain as part of the Getting started guide. If you chose a different domain for your example, make sure that you have an HTTPRoute that can be reached under the host you want to rewrite to.
    curl -vi http://$INGRESS_GW_ADDRESS:8080/headers -H "host: rewrite.example:8080"
    curl -vi localhost:8080/headers -H "host: rewrite.example"

    Example output:

    ...
    {
     "headers": {
       "Accept": [
         "*/*"
       ],
       "Host": [
         "www.example.com"
       ],
       "User-Agent": [
         "curl/7.77.0"
       ],
       "X-Envoy-Expected-Rq-Timeout-Ms": [
         "15000"
       ],
       "X-Forwarded-Proto": [
         "http"
       ],
       "X-Request-Id": [
         "ffc55a3e-60ae-4c90-9a5c-62c8a1ba1076"
       ]
     }
    }
  3. Optional: Clean up the resources that you created.

    kubectl delete httproute httpbin-rewrite -n httpbin