使用ALB Ingress Controller将非www重定向到www [英] Redirect non www to www using ALB Ingress Controller

查看:276
本文介绍了使用ALB Ingress Controller将非www重定向到www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将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.

我已经设置了httphttps重定向,并且可以正常工作.

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.

alb.ingress.kubernetes.io/actions.$ {action-name}提供了一种方法 在侦听器上配置自定义操作,例如用于重定向 动作.

alb.ingress.kubernetes.io/actions.${action-name} Provides a method for configuring custom actions on a listener, such as for Redirect Actions.

注释中的操作名称必须与 入口规则,并且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

alb.ingress.kubernetes.io/conditions.$ {conditions-name}提供了 除了原始路由之外还指定路由条件的方法 Ingress规范中的主机/路径条件.

alb.ingress.kubernetes.io/conditions.${conditions-name} Provides a method for specifying routing conditions in addition to original host/path condition on Ingress spec.

注释中的条件名称必须与中的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆