5

I am trying to redirect a non www request to www. I checked the annotations here https://kubernetes-sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/ but could not find specific to non www to www redirect.

I already have a http to https redirect set and it's working.

below is my ingress resource manifest file.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: eks-learning-ingress
  namespace: production
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/certificate-arn: ard878ef678df
    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"}}'
  labels:
    app: eks-learning-ingress
spec:
  rules:
  - host: www.myhost.in
    http:
      paths:
        - path: /*
          backend:
            serviceName: ssl-redirect
            servicePort: use-annotation
        - path: /*
          backend:
            serviceName: eks-learning-service
            servicePort: 80

Any help in this would be great. Thanks.

opensource-developer
  • 2,826
  • 4
  • 38
  • 88
  • Have you tried the [alb.ingress.kubernetes.io/actions](https://kubernetes-sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/#actions)? As it "Provides a method for configuring custom actions on a listener, such as for Redirect Actions." – Wytrzymały Wiktor Feb 27 '20 at 08:40
  • hi @OhHiMark, thanks for your reply. In my case since my domain is hosted on godaddy and i am unable to add a root / bare domain record pointing to DNS name of my ALB, i am not able to use actions. – opensource-developer Feb 28 '20 at 09:28

1 Answers1

11

There are 2 ways of doing non-www to www redirections, 1. using alb.ingress.kubernetes.io/actions, 2. alb.ingress.kubernetes.io/conditions.


alb.ingress.kubernetes.io/actions

alb.ingress.kubernetes.io/actions.${action-name} Provides a method for configuring custom actions on a listener, such as for Redirect Actions.

The action-name in the annotation must match the serviceName in the ingress rules, and servicePort must be use-annotation.

We need to have one more annotation, which tells ALB how to configure redirection:

alb.ingress.kubernetes.io/actions.redirect-to-www: >
    {"Type":"redirect","RedirectConfig":{"Host":"www.myhost.in","Port":"443","Protocol":"HTTPS","StatusCode":"HTTP_302"}}

And one more host rule to catch your request domain myhost.in and redirect to www.myhost.in

- host: myhost.in
  http:
    paths:
      - path: /*
        backend:
          serviceName: redirect-to-www
          servicePort: use-annotation

alb.ingress.kubernetes.io/conditions

alb.ingress.kubernetes.io/conditions.${conditions-name} Provides a method for specifying routing conditions in addition to original host/path condition on Ingress spec.

The conditions-name in the annotation must match the serviceName in the ingress rules. It can be a either real serviceName or an annotation based action name when servicePort is "use-annotation".

In addition to the annotation we have added above, we continue to add conditions to annotations to filter the request but no need to have the host rule above.

alb.ingress.kubernetes.io/actions.redirect-to-www: >
    {"Type":"redirect","RedirectConfig":{"Host":"www.myhost.in","Port":"443","Protocol":"HTTPS","StatusCode":"HTTP_302"}}
alb.ingress.kubernetes.io/conditions.redirect-to-www: >
    [{"Field":"host-header","HostHeaderConfig":{"Values":["myhost.in"]}}]

We modify the existing host rule you currently have to achieve the redirection.

- host: www.myhost.in
    http:
      paths:
        - path: /*
          backend:
            serviceName: redirect-to-www
            servicePort: use-annotation
        - path: /*
          backend:
            serviceName: ssl-redirect
            servicePort: use-annotation
        - path: /*
          backend:
            serviceName: eks-learning-service
            servicePort: 80

Update for networking.k8s.io/v1

- host: www.myhost.in
  http:
    paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: redirect-to-www
            port:
              name: use-annotation
wpater
  • 393
  • 3
  • 14
Fred Lai
  • 1,131
  • 10
  • 21