Tangible Bytes

A Web Developer’s Blog

Nginx Ingress Redirect Fromto Www

I keep forgetting the details of this and for some reason the docs seem to be hard to find …

If you have a website on say https://tangiblebytes.co.uk and you want requests for https://www.tangiblebytes.co.uk to be redirected there you need

https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/nginx-configuration/annotations.md#redirect-fromto-www

The ingress then looks like

ingress:
  enabled: true
  className: nginx
  annotations: 
    cert-manager.io/cluster-issuer: letsencrypt-production
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
  hosts:
    - host: tangiblebytes.co.uk
      paths:
        - path: /
          pathType: ImplementationSpecific
  tls: 
    - secretName: website-tls
      hosts:
      - www.tangiblebytes.co.uk
      - tangiblebytes.co.uk

Not that you define only the host you want it to redirect to - so include the www here if you want this to be the canonical version.

You do need tls certs for both - so include both the ine tls section (order doesn’t matter)