Kubernetes nginx ingress controller Redirect

9/3/2021

I have a website deployed in Kubernetes and ingress controller working.

I need to redirect my old subdomain to the new subdomain(old.example.com -> new.example.com).

I made some research and found that I have to use the annotation:

nginx.ingress.kubernetes.io/rewrite-target

my ingress file:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: rancher-ing
  namespace: test
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: letsencrypt-prod
    kubernetes.io/tls-acme: "true"
    ingress.kubernetes.io/secure-backends: "true"
    ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1

spec:
  tls:
  - hosts:
    - new.example.com
    secretName: new.example.com
  rules:
  - host: new.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: rancher-logo-service
            port:
              number: 80
  - host: old.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: rancher-logo-service
            port:
              number: 80
-- gharbi.bdr
kubernetes
kubernetes-ingress
nginx

1 Answer

9/6/2021

i created an ingress controller config for the new url. and updated the old config into this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: rancher-ing-redirect
  namespace: test
  annotations:
    kubernetes.io/ingress.class: "nginx"
    cert-manager.io/cluster-issuer: letsencrypt-prod
    kubernetes.io/tls-acme: "true"
    ingress.kubernetes.io/secure-backends: "true"
    ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/rewrite-target: https://new.example.com/
spec:
  rules:
  - host: old.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: rancher-logo-service
            port:
              number: 80
-- gharbi.bdr
Source: StackOverflow