5

My ingress currently looks like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - example.org
        - app.example.org
      secretName: prod-tls
  rules:
    - host: example.org
      http:
        paths:
          - path: /
            backend:
              serviceName: app-service
              servicePort: 80
    - host: app.example.org
        http:
          paths:
              - path: /
                backend:
                  serviceName: app-service
                  servicePort: 80

But now I want to redirect app.example.org to example.org instead. How can I do this?

I found this example using ingress.kubernetes.io/configuration-snippet but I don't know what domains that will apply to?

I'm using Helm nginx-ingress-1.37.0; app ver 0.32.0.

mpen
  • 313
  • 5
  • 14

3 Answers3

5

I found this example using ingress.kubernetes.io/configuration-snippet: but I don't know what domains that will apply to?

It's easier to reason about that configuration when you have two Ingresses, one just for "hosting" the configuration-snippet: and the other for doing the real work:

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - example.org
      secretName: prod-tls
  rules:
    - host: example.org
      http:
        paths:
          - path: /
            backend:
              serviceName: app-service
              servicePort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-redirect
  annotations:
    kubernetes.io/ingress.class: nginx
    # this should be covered by the annotation on the other ingress
    # since it will renew the same certificate
    # cert-manager.io/cluster-issuer: letsencrypt-prod
    ingress.kubernetes.io/configuration-snippet: |
        rewrite ^/(.*)$ https://example.org/$1 permanent;
spec:
  tls:
    - hosts:
        - app.example.org
      secretName: prod-tls
  rules:
    - host: app.example.org
      http:
          paths:
              - path: /
                backend:
                  serviceName: app-service
                  servicePort: 80
mdaniel
  • 2,338
  • 1
  • 8
  • 13
3

For Kubernetes 1.19 and up, this ingress will redirect all traffic from app.example.org to example.org:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      return 301 $scheme://example.org$request_uri;
  name: ingress-redirect
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - app.example.org
    secretName: prod-tls
  rules:
  - host: app.example.org
gsf
  • 131
  • 3
2

Ingress controller version 0.22.0 or higher uses nginx.ingress.kubernetes.io/rewrite-target instead of ingress.kubernetes.io/configuration-snippet. So this would be the new for the @mdaniel answer:


apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  tls:
    - hosts:
        - example.org
      secretName: prod-tls
  rules:
    - host: example.org
      http:
        paths:
          - path: /
            backend:
              serviceName: app-service
              servicePort: 80
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-redirect
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: "https://example.org/$1"
spec:
  tls:
    - hosts:
        - app.example.org
      secretName: prod-tls
  rules:
    - host: app.example.org
      http:
          paths:
              - path: /
                backend:
                  serviceName: app-service
                  servicePort: 80
Mauricio
  • 203
  • 1
  • 2
  • 8
  • Use `nginx.ingress.kubernetes.io/permanent-redirect: https://example.org` to keep 301 permanent redirect https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#permanent-redirect – Morgan Christiansson Jul 23 '22 at 04:38