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 Gloo Gateway.

  2. Follow the Sample app guide to create a 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.

In-cluster service host rewrites

  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: gloo-system
      hostnames:
        - rewrite.example
      rules:
         - filters:
           - type: URLRewrite
             urlRewrite:
               hostname: "www.example.com"
           backendRefs:
            - name: httpbin
              port: 8000
    EOF
      
    SettingDescription
    spec.parentRefsThe 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.typeThe type of filter that you want to apply to incoming requests. In this example, the URLRewrite filter is used.
    spec.rules.filters.urlRewrite.hostnameThe hostname that you want to rewrite requests to.
    spec.rules.backendRefsThe 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.

    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"
       ]
     }
    }
      

External service host rewrites

  1. Create a Backend that represents your external service. The following example creates a Backend for the httpbin.org domain.

      kubectl apply -f- <<EOF
    apiVersion: gateway.kgateway.dev/v1alpha1
    kind: Backend
    metadata:
      name: httpbin
      namespace: default
    spec:
      type: Static
      static:
        hosts:
          - host: httpbin.org
            port: 80
    EOF
      
  2. Create an HTTPRoute resource that matches incoming traffic on the external-rewrite.example domain and forwards traffic to the Backend that you created. Because the Backend expects a different domain, you use the URLRewrite filter to rewrite the hostname from external-rewrite.example to httpbin.org.

      kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: backend-rewrite
      namespace: default
    spec:
      parentRefs:
      - name: http
        namespace: gloo-system
      hostnames:
        - external-rewrite.example
      rules:
         - filters:
           - type: URLRewrite
             urlRewrite:
               hostname: "httpbin.org"
           backendRefs:
           - name: httpbin
             kind: Backend
             group: gateway.kgateway.dev
    EOF
      
  3. Send a request to the external-rewrite.example domain. Verify that you get back a 200 HTTP response code and that you see the Host: httpbin.org header in your response.

    Example output:

      * Request completely sent off
    < HTTP/1.1 200 OK
    HTTP/1.1 200 OK
    < content-type: application/json
    content-type: application/json
    < content-length: 268
    content-length: 268
    < server: envoy
    server: envoy
    < access-control-allow-origin: *
    access-control-allow-origin: *
    < access-control-allow-credentials: true
    access-control-allow-credentials: true
    < x-envoy-upstream-service-time: 2416
    x-envoy-upstream-service-time: 2416
    < 
    
    {
      "headers": {
        "Accept": "*/*", 
        "Host": "httpbin.org", 
        "User-Agent": "curl/8.7.1", 
        "X-Amzn-Trace-Id": "Root=1-6859932a-1fb1dc706dadcc183b407d4d", 
        "X-Envoy-Expected-Rq-Timeout-Ms": "15000", 
       "X-Envoy-External-Address": "10.0.15.215"
      }
    }   
      

Cleanup

You can remove the resources that you created in this guide.
  kubectl delete httproute httpbin-rewrite -n httpbin
kubectl delete httproute backend-rewrite 
kubectl delete backend httpbin