Home > front end >  Route non-www domain to www in EKS ingress
Route non-www domain to www in EKS ingress

Time:07-12

I want to redirect my domain from non-www to www. I am using EKS and ALB ingress controllers. Following is my ingress file.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: test
  name: test
  annotations:
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: instance
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:ap-south-1:851874770356:certificate/ebdbe-4b05-b0a5-e8238533,arn:aws:acm:ap-south-1:8518746:certificate/734ffa35-3e17-497c-b5ca-5719e
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS":443}]'
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
    alb.ingress.kubernetes.io/actions.redirect-to-www: >
      {"Type":"redirect","RedirectConfig":{"Host":"www.test.com","Port":"443","Protocol":"HTTPS","StatusCode":"HTTP_302"}}


spec:
  rules:
    - host: test.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: ssl-redirect
                port:
                  name: use-annotation
          - path: /
            pathType: Prefix
            backend:
              service:
                name: redirect-to-www
                port:
                  name: use-annotation

          - path: /
            pathType: Prefix
            backend:
              service:
                name: "dashboard"
                port:
                  number: 3000

Now when I access test.com it is redirecting to https://www.test.com and then gets "HTTP ERROR 404".

I have tried to re-name my host rule to www.test.com but after that, the redirection is not working.

May I know what is wrong with my configuration?

CodePudding user response:

Try creating a new rule, which matches www.test.com and move the dashboard service description in that rule. This way, the redirections will work, and when an https://www.test.com/ request will be received, the second rule will be invoked, forwarding it to the dashboard service. Like this:

spec:
  rules:
    - host: test.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: ssl-redirect
                port:
                  name: use-annotation
          - path: /
            pathType: Prefix
            backend:
              service:
                name: redirect-to-www
                port:
                  name: use-annotation

    - host: www.test.com
      https:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: "dashboard"
                port:
                  number: 3000
  • Related