20

In a classic Kubernetes Nginx ingress I know that it is possible to rewrite target url based on a specific regex by appling this annotation nginx.ingress.kubernetes.io/rewrite-target: /$1

But this annotation does not work in AWS ALB ingress. Does anyone know if it is possible to do rewriting work with this kind of ingress?

Martin Tovmassian
  • 1,010
  • 1
  • 10
  • 19

3 Answers3

10

Ok, it seems not supported at this time => https://github.com/kubernetes-sigs/aws-alb-ingress-controller/issues/835

Martin Tovmassian
  • 1,010
  • 1
  • 10
  • 19
  • 1
    To clarify for anyone coming to this in 2023 - this answer/link are still up to date. ALB still does not support URL rewriting. The other answers show how to do redirection, not URL rewriting. – PsylentKnight Aug 08 '23 at 21:00
-1

Add action annotation:

alb.ingress.kubernetes.io/actions.redirect-home: '{"Type":"redirect","RedirectConfig": {"Host":"abc.example.com","Path":"/mycontext/other-path","Port":"443","Protocol":"HTTPS","Query":"#{query}","StatusCode":"HTTP_301"}}'

Add route to the action:

      - backend:
          service:
            name: redirect-home
            port:
              name: use-annotation
        path: /some-path
        pathType: ImplementationSpecific

This will redirect /some-path to abc.example.com/mycontext/other-path

thanks

-1

Once my requirement was to redirect traffic coming to coffee.abc.com from coffee.xyz.com So for this, I used alb redirect annotation for it.

alb.ingress.kubernetes.io/actions.coffee-redirection: '{"Type":"redirect","RedirectConfig":{"Host":"coffee.xyz.com","Port":"443","Protocol":"HTTPS","Query":"#{query}","StatusCode":"HTTP_301"}}'

Below is complete code block for the same issue.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig":{ "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:**********************************************
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP":80},{"HTTPS":443}]'
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/subnets: subnet-*******, subnet-********, subnet-**********,subnet-********
    service.beta.kubernetes.io/aws-load-balancer-name: coffee-alb-staging
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/ssl-redirect: '443'
    alb.ingress.kubernetes.io/group.name: coffee-staging
    alb.ingress.kubernetes.io/actions.coffee-redirection: '{"Type":"redirect","RedirectConfig":{"Host":"coffee.xyz.com","Port":"443","Protocol":"HTTPS","Query":"#{query}","StatusCode":"HTTP_301"}}'
  name: coffee-alb-staging
  namespace: NameSpace
spec:
rules:
- host: coffee.abc.com
http:
  paths:
    - path: /
      pathType: Prefix
      backend:
        service:
          name: coffee-fe
          port: 
            number: 80
    - path: /
      pathType: Exact
      backend:
        service:
          name: coffee-redirection
          port: 
            name: use-annotation