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.

Create redirect URLs

Page as Markdown

Extract the values of common pseudo headers to generate a redirect URL.

About pseudo headers

Pseudo headers are special headers that are used in HTTP/2 to provide metadata about the request or response in a structured way. Although they look like traditional HTTP/1.x headers, they come with specific characteristics:

  • Pseudo headers must always start with a colon (:).
  • They must appear before regular headers in the HTTP/2 frame.
  • Pseudo headers contain details about the request or response.

Common pseudo headers include:

  • :method: The HTTP method that is used, such as GET or POST.
  • :scheme: The protocol that is used, such as http or https.
  • :authority: The hostname and port number that the request is sent to.
  • :path: The path of the request.

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

Use pseudo headers for redirect URLs

  1. Create a RouteOption or VirtualHostOption resource that defines the following transformation rules:

    • Build a redirect URL with the values of the :authority and :path pseudo headers. These headers are extracted from the request with the request_header function that is provided in Gloo Gateway.
    • The :authority pseudo header contains the hostname that the request is sent to.
    • The :path pseudo header is set to the request path.
    • The redirect URL is added to the x-forwarded-uri response header.
      kubectl apply -f- <<EOF
      apiVersion: gateway.solo.io/v1
      kind: RouteOption
      metadata:
        name: transformation
        namespace: httpbin
      spec:
        targetRefs:
        - group: gateway.networking.k8s.io
          kind: HTTPRoute
          name: httpbin
        options:
          transformations:
            responseTransformation:
              transformationTemplate:
                 headers:
                   x-forwarded-uri:
                     text: 'https://{{ request_header(":authority") }}{{ request_header(":path") }}'
                 parseBodyBehavior: "DontParse"
      EOF
      kubectl apply -n gloo-system -f- <<EOF
      apiVersion: gateway.solo.io/v1
      kind: VirtualHostOption
      metadata:
        name: transformation
        namespace: gloo-system
      spec:
        options:
          transformations:
            responseTransformation:
              transformationTemplate:
                headers:
                  x-forwarded-uri:
                    text: 'https://{{ request_header(":authority") }}{{ request_header(":path") }}'
                parseBodyBehavior: "DontParse"
        targetRefs:
        - group: gateway.networking.k8s.io
          kind: Gateway
          name: http
          namespace: gloo-system
      EOF
  2. Send a request to the httpbin app. Verify that you see the x-forwarded-uri response header that is set to the hostname and path that was used in the initial request.

    curl -vik -H "host: www.example.com:8080" \
      -H "Content-Type: application/json" \
      http://$INGRESS_GW_ADDRESS:8080/response-headers
    curl -vik localhost:8080/response-headers \
      -H "host: www.example.com" \
      -H "Content-Type: application/json" \

    Example output:

    ...
    * Request completely sent off
    < HTTP/1.1 200 OK
    HTTP/1.1 200 OK
    < access-control-allow-credentials: true
    access-control-allow-credentials: true
    < access-control-allow-origin: *
    access-control-allow-origin: *
    ...
    < x-forwarded-uri: https://www.example.com:8080/response-headers
    x-forwarded-uri: https://www.example.com:8080/response-headers
    < server: envoy
    server: envoy
    < 

Cleanup

You can remove the resources that you created in this guide.

kubectl delete virtualhostoption transformation -n gloo-system
kubectl delete routeoption transformation -n httpbin