amazon-web-services - 使用 ALB 入口 Controller 将非 www 重定向到 www

标签 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重定向设置,它的工作。

下面是我的入口资源 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/

相关文章:

amazon-web-services - 如何使用 API Gateway 调用 AWS Step Function

amazon-web-services - CloudWatch 的集成服务名称是什么

linux - 交换 :/swapfile: swapoff failed: Cannot allocate memory

amazon-web-services - 有没有办法在 "all"请求的 AWSALB cookie 中编码关于 "one"目标组的 session 粘性信息?

amazon-ec2 - 当实例初始化后启动进程时,有没有办法在我的 ec2 实例上公开端口?

amazon-web-services - Elastic beanstalk - eb create 无法创建 AWSEBRDSDBSecurityGroup

amazon-web-services - Cognito 用户池是否可以将 SES 与经过验证的域而不是经过验证的电子邮件一起使用?

amazon-web-services - Kubernetes:删除后,Replication Controller仍然存在

kubernetes - 删除部署会留下尾随的副本集和 Pod

amazon-web-services - AWS ALB - 所有服务都是单一的?