使用 ALB 入口控制器将非 www 重定向到 www

ope*_*per 2 amazon-web-services kubernetes aws-application-load-balancer

我正在尝试将non www请求重定向到www. 我在这里检查了注释https://kubernetes-sigs.github.io/aws-alb-ingress-controller/guide/ingress/annotation/但找不到特定于非 www 到 www 的重定向。

我已经有一个httphttps重定向设置,它的工作。

下面是我的入口资源清单文件。

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
Run Code Online (Sandbox Code Playgroud)

这方面的任何帮助都会很棒。谢谢。

Fre*_*red 6

有两种方法可以进行非 www 到 www 的重定向,1. 使用alb.ingress.kubernetes.io/actions2. alb.ingress.kubernetes.io/conditions.


alb.ingress.kubernetes.io/actions

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

annotation 中的 action-name 必须与 ingress 规则中的 serviceName 匹配,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"}}
Run Code Online (Sandbox Code Playgroud)

还有一个主机规则来捕获您的请求域myhost.in并重定向到www.myhost.in

- host: myhost.in
  http:
    paths:
      - path: /*
        backend:
          serviceName: redirect-to-www
          servicePort: use-annotation
Run Code Online (Sandbox Code Playgroud)

alb.ingress.kubernetes.io/conditions

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

注释中的 conditions-name 必须与入口规则中的 serviceName 匹配。当 servicePort 是“use-annotation”时,它可以是真实的 serviceName 或基于注释的操作名称。

除了我们上面添加的注解之外,我们继续在注解中添加条件来过滤请求,但不需要有上面的主机规则。

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"]}}]
Run Code Online (Sandbox Code Playgroud)

我们修改了您当前必须实​​现重定向的现有主机规则。

- 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
Run Code Online (Sandbox Code Playgroud)