问题描述
我正在尝试将non www
请求重定向到www
.我在这里 https://kubernetes- sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/,但找不到特定于非www到www重定向.
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.
我已经设置了http
到https
重定向,并且可以正常工作.
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.
推荐答案
执行从非www到www重定向的方法有两种,一种是使用alb.ingress.kubernetes.io/actions
,第二种是alb.ingress.kubernetes.io/conditions
.
There are 2 ways of doing non-www to www redirections, 1. using alb.ingress.kubernetes.io/actions
, 2. alb.ingress.kubernetes.io/conditions
.
注释中的操作名称必须与 入口规则,并且servicePort必须为使用注释.
The action-name in the annotation must match the serviceName in the ingress rules, and servicePort must be use-annotation.
我们需要再添加一个注释,它告诉ALB如何配置重定向:
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"}}
还有另一条主机规则来捕获您的请求域myhost.in
并重定向到www.myhost.in
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
注释中的条件名称必须与中的serviceName相匹配 进入规则.它可以是真实的serviceName或 当servicePort为"use-annotation"时,基于注释的操作名称.
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
这篇关于使用ALB Ingress Controller将非www重定向到www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!