我正在尝试重定向 non www
请求到 www
.我在这里查看了注释 https://kubernetes-sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/但找不到特定于非 www 到 www 的重定向。
我已经有 http
至 https
重定向设置,它的工作。
下面是我的入口资源 list 文件。
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
这方面的任何帮助都会很棒。谢谢。
最佳答案
有两种方法可以进行非 www 到 www 的重定向,1. 使用 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.
我们还需要一个注解,它告诉 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
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".
除了我们上面添加的注解之外,我们继续在注解中添加条件来过滤请求,但不需要有上面的主机规则。
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
关于amazon-web-services - 使用 ALB 入口 Controller 将非 www 重定向到 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60413536/