Configure HTTP to HTTPS redirect using traefik's IngressRoute

traefik redirect http https IngressRoute

2 min read | by Jordi Prats

When using the Traefik ingress controller there are several ways of redirecting HTTP traffic to HTTPS, one of the ways is using IngressRoute (Traefik 2)

If we want to redirect traffic for a specific domain we can configure a rule for that domain as follows:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: website-http-to-https
spec:
  entryPoints:
    - web
  routes:
    - match: Host(`pet2cattle.com`)
      priority: 1000
      kind: Rule
      services:
        - kind: TraefikService
          name: noop@internal
      middlewares:
        - name: website-http-to-https
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: website-http-to-https
spec:
  redirectScheme:
    scheme: https
    permanent: true

On the other hand, if we want to redirect it globally for all the Ingress that we have defined, we can still create an IngressRoute matching all the possible URLs (for example, all of them are going to have / as a path prefix)

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: global-http-to-https
spec:
  entryPoints:
    - web
  routes:
    - match: PathPrefix(`/`)
      priority: 1000
      kind: Rule
      services:
        - kind: TraefikService
          name: noop@internal
      middlewares:
        - name: global-http-to-https
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: global-http-to-https
spec:
  redirectScheme:
    scheme: https
    permanent: true

To redirect HTTP to HTTPS globally there are several ways like set it on the helm chart, depending on the environment one or the other will make more sense.


Posted on 07/10/2022