Kubernetes ingress nginx redirecting domain and also apply to all redirected domain's subdomains

Viewed 3677

I want to redirect the main domain in Kubernetes ingress Nginx but would like subdomains to get applied to the redirected main domain.

Here is the example website: my.website.com

Inside this website there are many subdomains which are /abc, /def, /abc/aa, /def/bb, etc.

For my Kubernetes ingress Nginx, I want to redirect my.website.com to this.website.com and also would like all subdomains to redirect to this.website.com

Here is the example that I want:

my.website.com/abc to this.website.com/abc

Here is the redirect ingress yaml that I tried:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/permanent-redirect: https://this.website.com
  name: redirect-subdomains-too
spec:
  rules:
  - host: my.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: /

Obviously, when I try my.website.com/abc OR my.website.com/abc/aa this will redirect to this.website.com not to this.website.com/abc OR this.website.com/abc/aa.

How can I script this ingress?

2 Answers

The permanent redirect is like 'hard code' for everything. You probably want rewrite and/or a server-snippet. Then you can have multiple host rules for each of the hosts:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/server-snippet: |
        if ($host ~ "my.website.com|my2.website.com")
        {
            rewrite ^ https://this.website.com$request_uri permanent;
        }
  name: redirect-subdomains-too
spec:
  rules:
  - host: my.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: (/|$)(.*)
  - host: my2.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: (/|$)(.*)
  - host: this.website.com
    http:
      paths:
      - backend:
          serviceName: redirect-service
          servicePort: 7990
        path: (/|$)(.*)

You can always check the config by running:

kubectl exec -t <nginx-ingress-pod> cat nginx.conf

you just need an ingress wich make a redirect to another URL...

for example.. you have an ingress called adonai.domain.com, and another called, eggs.domain.com. when try get http://eggs.domain.com you need to redirect to http://adonai.domain.com right? do this;

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      annotations:
            nginx.ingress.kubernetes.io/permanent-redirect: http://adonai.domain.com
      name: web2
      namespace: default
    spec:
      ingressClassName: nginx
      rules:
      - host: ovo.domain.com

You don't need backend or another setting in spec.rules. just host to receive and redirect, ingress-nginx-controller does the rest for you.

Related