This project is in the process of being donated to the CNCF and is not affiliated with the Kubernetes project.

HTTPS redirect

Permanently redirect HTTP traffic to HTTPS.

For more information, see the Kubernetes Gateway API documentation.

Before you begin

  1. Follow the Get started guide to install K8sGateway, 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

Redirect HTTP traffic to HTTPS

  1. Create an HTTP route for the httpbin app that you set up as part of the Get started guide. In the following example, all HTTP requests are redirected to HTTPS, and a 301 HTTP response code is returned to the user.

    kubectl apply -f- <<EOF
    apiVersion: gateway.networking.k8s.io/v1
    kind: HTTPRoute
    metadata:
      name: httpbin-https-redirect
      namespace: httpbin
      labels:
        example: httpbin-route
    spec:
      parentRefs:
        - name: http
          namespace: gloo-system
      hostnames: 
        - redirect.example
      rules:
        - filters:
          - type: RequestRedirect
            requestRedirect:
              scheme: https
              statusCode: 301
    EOF  
    Setting Description
    spec.parentRefs.name The name and namespace of the gateway resource that serves the route. In this example, you use the gateway that you installed as part of the Get started guide.
    spec.hostnames The hostname for which you want to apply the redirect.
    spec.rules.filters.type The type of filter that you want to apply to incoming requests. In this example, the RequestRedirect is used.
    spec.rules.filters.requestRedirect.scheme The type of redirect that you want to apply. The https scheme redirects all incoming traffic to HTTPS.
    spec.rules.filters.requestRedirect.statusCode The HTTP status code that you want to return to the client in case of a redirect. For a permanent redirect, use the 301 HTTP status code.
  2. Send a request to the httpbin app on the redirect.example domain. Verify that you get back a 301 HTTP response code and that your redirect location shows https://redirect.example:8080/status/200.

    curl -vik http://$INGRESS_GW_ADDRESS:8080/status/200 -H "host: redirect.example"
    curl -vik localhost:8080/status/200 -H "host: redirect.example"

    Example output:

    * Mark bundle as not supporting multiuse
    < HTTP/1.1 301 Moved Permanently
    HTTP/1.1 301 Moved Permanently
    < location: https://redirect.example:8080/status/200
    location: https://redirect.example:8080/status/200
    < date: Mon, 06 Nov 2023 01:48:12 GMT
    date: Mon, 06 Nov 2023 01:48:12 GMT
    < server: envoy
    server: envoy
    < content-length: 0
    content-length: 0

Cleanup

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