我正在尝试将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如何配置重定向:
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/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 Ingress Controller将非www重定向到www,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60413536/