Skip to content
If you are interested in trying out Gloo Gateway with the Kubernetes Gateway API, check out Solo Enterprise for kgateway. This version adds enterprise functionality on top of the kgateway open source project.

Host rewrites

Page as Markdown

Replace the host header value before forwarding a request to a backend service.

For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Follow the Get started guide to install Gloo Gateway, set up a gateway resource, and deploy the httpbin sample app.

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

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

Rewrite hosts

Path rewrites use the HTTP path modifier to rewrite path prefixes.

  1. Create a RouteOption resource to define your rewrite rules. In the following example the host request header is rewritten to the www.example.com host.

    kubectl apply -n httpbin -f- <<EOF
    apiVersion: gateway.solo.io/v1
    kind: RouteOption
    metadata:
      name: rewrite
      namespace: httpbin
    spec:
      options:
        hostRewrite: 'www.example.com'
    EOF
  2. Create an HTTPRoute resource for the httpbin app that references the RouteOption resource that you created. In this example, all incoming requests on the rewrite.example domain are rewritten to the www.example.com host as defined in the referenced RouteOption resource.

    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: ExtensionRef
             extensionRef:
               group: gateway.solo.io
               kind: RouteOption
               name: rewrite
           backendRefs:
            - name: httpbin
              port: 8000
    EOF
  3. 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.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/headers -H "host: rewrite.example:8080"
    curl -vik 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"
       ]
     }
    }
  4. Optional: Clean up the resources that you created.

    kubectl delete routeoption rewrite -n httpbin
    kubectl delete httproute httpbin-rewrite -n httpbin