4

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?

Rico
  • 58,485
  • 12
  • 111
  • 141
JShine
  • 41
  • 1
  • 3
  • This should be helpful https://stackoverflow.com/questions/60413536/redirect-non-www-to-www-using-alb-ingress-controller – Fred Lai Jul 14 '20 at 21:26

2 Answers2

1

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
ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66
Rico
  • 58,485
  • 12
  • 111
  • 141
  • The `server-snippet` is close, but adding `$request_uri` results in duplicate query parameters. Unfortunately, nginx does not seem to have a variable like `$request_path`, which would be ideal here. I read that `$uri` is nearly the request path, but can be modified by nginx. – Cameron Hudson Oct 17 '21 at 19:03
  • I was able to use this with success, however: `"nginx.ingress.kubernetes.io/server-snippet" = "if ($host ~ \"www.mywebsite.com\") { return 308 https://mywebsite.com$request_uri; }"` – Cameron Hudson Oct 17 '21 at 20:02
  • This seems to break cert-manager though, presumably because validation requests from Let's Encrypt get redirected – Hubro Jun 05 '22 at 13:17
0

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.