有 2 种方法可以进行非 www 到 www 的重定向, 1. 使用alb.ingress.kubernetes.io/actions
, 2. alb.ingress.kubernetes.io/conditions
.
alb.ingress.kubernetes.io/actions https://kubernetes-sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/#actions
alb.ingress.kubernetes.io/actions.${action-name} 提供了一种方法
在侦听器上配置自定义操作,例如重定向
行动。
注释中的操作名称必须与注释中的服务名称匹配
入口规则,servicePort 必须是 use-annotation。
我们还需要一个注释,它告诉 ALB 如何配置重定向:
alb.ingress.kubernetes.io/actions.redirect-to-www: >
{"Type":"redirect","RedirectConfig":{"Host":"www.myhost.in","Port":"443","Protocol":"HTTPS","StatusCode":"HTTP_302"}}
还有一项主机规则来捕获您的请求域myhost.in
并重定向到www.myhost.in
- host: myhost.in
http:
paths:
- path: /*
backend:
serviceName: redirect-to-www
servicePort: use-annotation
alb.ingress.kubernetes.io/conditions https://kubernetes-sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/#conditions
alb.ingress.kubernetes.io/conditions.${conditions-name} 提供
除原始路由条件外还指定路由条件的方法
Ingress 规范上的主机/路径条件。
注释中的条件名称必须与中的服务名称匹配
入口规则。它可以是真实的 serviceName 或
当 servicePort 为“use-annotation”时,基于注释的操作名称。
除了上面我们添加的注解之外,我们继续在注解中添加条件来过滤请求,但是不需要上面的host规则。
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"]}}]
我们修改您当前必须实现的重定向的现有主机规则。
- 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
更新为networking.k8s.io/v1
- host: www.myhost.in
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: redirect-to-www
port:
name: use-annotation