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.

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/v1beta1
    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  
      
    SettingDescription
    spec.parentRefs.nameThe 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.hostnamesThe hostname for which you want to apply the redirect.
    spec.rules.filters.typeThe type of filter that you want to apply to incoming requests. In this example, the RequestRedirect is used.
    spec.rules.filters.requestRedirect.schemeThe type of redirect that you want to apply. The https scheme redirects all incoming traffic to HTTPS.
    spec.rules.filters.requestRedirect.statusCodeThe 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.

    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 optionally delete the HTTP route that you created as part of this guide.

  kubectl delete httproute httpbin-https-redirect -n httpbin