更新时间:2024-04-23 GMT+08:00
分享

ELB Ingress配置URL重定向

Ingress支持将特定的访问请求重定向至指定的路径。配置Ingress重定向规则的YAML示例如下,本示例将访问example.com的请求重定向至指定路径example.com/testa,并返回301状态码。

约束与限制

  • ELB Ingress高级转发策略仅支持使用独享型ELB。
  • ELB Ingress高级转发策略支持的集群版本为v1.23.14-r0、v1.25.9-r0、v1.27.6-r0、v1.28.4-r0及以上。

使用YAML配置Ingress重定向至URL规则

Ingress重定向至URL可通过注解实现,示例如下:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-redirect-url
  namespace: default
  annotations:
    kubernetes.io/elb.id: df76342f-e898-402a-bac8-bde5bf974da8
    kubernetes.io/elb.class: performance
    kubernetes.io/elb.port: '80'
    kubernetes.io/elb.redirect-url: https://example.com/testa        # 重定向URL的信息
    kubernetes.io/elb.redirect-url-code: '301'     # 重定向URL后的返回码
spec:
  rules:
    - host: "example.com"
      http:
        paths:
          - path: /
            backend:
              service:
                name: test-service
                port:
                  number: 8888
            property:
              ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH
            pathType: ImplementationSpecific
  ingressClassName: cce
表1 关键参数说明

参数

是否必填

参数类型

描述

kubernetes.io/elb.redirect-url

string

重定向URL信息。

格式说明:以 "http://" 或 "https://" 开头的合法的URL,如 https://example.com/。

参数说明:对单个Ingress下所有的转发规则均生效,配置删除后自动清理对应的重定向URL规则。

该注解不能和灰度发布的注解一起配置。

kubernetes.io/elb.redirect-url-code

string

重定向URL后的返回码。

格式说明:支持返回码包括:"301"、"302"、"303"、"307"、"308"。

参数说明:默认值为"301"。

使用curl测试重定向后的能力,其中${ELB_IP}为Ingress访问的IP:

# curl -I -H "Host:example.com" {$ELB_IP}

最终访问路径会被重定向至example.com/testa。

HTTP/1.1 301 Moved Permanently
Date: Thu, 07 Mar 2024 11:04:31 GMT
Content-Type: text/html
Content-Length: 134
Connection: keep-alive
Location: https://example.com/testa
Server: elb
分享:

    相关文档

    相关产品