你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于容器的 Azure 应用程序网关的 URL 重定向 - 流入量 API

使用适用于容器的应用程序网关,可以基于 URL 的三个方面向客户端返回重定向响应:协议、主机名和路径。 对于每个重定向,可将定义的 HTTP 状态代码返回到客户端,以定义重定向的性质。

使用情况详细信息

URL 重定向利用 Kubernetes 网关 API 定义的 RequestRedirect 规则筛选器

重定向

重定向会设置返回到客户端的响应状态代码,以便客户端理解重定向的目的。 支持以下类型的重定向:

  • 301(永久移动):指示已为目标资源分配新的永久 URI。 以后对该资源的任何引用都将使用包含的 URI 之一。 将 301 状态代码用于 HTTP 到 HTTPS 重定向。
  • 302(找到) :指示目标资源暂时驻留在其他 URI 下。 由于重定向有时会发生更改,因此客户端应继续对将来的请求使用有效的请求 URI。

重定向功能

  • 协议重定向通常用于告知客户端从未加密的流量方案转移到流量,例如 HTTP 到 HTTPS 重定向。

  • 主机名重定向与请求的完全限定域名 (fqdn) 匹配。 这通常用于将旧域名重定向到新域名;如 contoso.com 重定向到 fabrikam.com

  • 路径重定向有两种不同的变体:prefixfull

    • Prefix 重定向类型会重定向以定义的值开头的所有请求。 例如,前缀 /shop 与 /shop 及其后的任何文本匹配。 例如,/shop、/shop/checkout 和 /shop/item-a 都会重定向到 /shop。
    • Full 重定向类型与确切值匹配。 例如,/shop 可以重定向到 /store,但 /shop/checkout 不会重定向到 /store。

下图演示了一个请求示例,发往 contoso.com/summer-promotion 的请求被重定向到 contoso.com/shop/category/5。 此外,通过 http 协议向 contoso.com 发起的第二个请求会返回一个重定向,以发起到其 https 变体的新连接。

显示用于容器的应用程序网关将重定向 URL 返回到客户端的关系图。

先决条件

  1. 如果遵循 BYO 部署策略,请确保设置适用于容器的应用程序网关资源和 ALB 控制器

  2. 如果遵循 ALB 托管部署策略,请确保预配 ALB 控制器,并通过 ApplicationLoadBalancer 自定义资源预配适用于容器的应用程序网关资源。

  3. 部署示例 HTTP 应用程序:

    在群集上应用以下 deployment.yaml 文件来部署示例 TLS 证书,从而演示重定向功能。

    kubectl apply -f https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/refs/heads/main/articles/application-gateway/for-containers/examples/https-scenario/ssl-termination/deployment.yaml
    

    此命令在群集上创建以下内容:

    • 名为test-infra的命名空间
    • test-infra命名空间中一项名为echo的服务
    • test-infra命名空间中一项名为echo的部署
    • test-infra命名空间中一个名为listener-tls-secret的机密

部署所需的 IngressExtension 资源

  1. 创建 IngressExtension 资源以处理 contoso.com 的 HTTP 到 HTTPS 重定向

    kubectl apply -f - <<EOF
    apiVersion: alb.networking.azure.io/v1
    kind: IngressExtension
    metadata:
      name: http-to-https
      namespace: test-infra
    spec:
      rules:
        - host: contoso.com
          requestRedirect:
            statusCode: 301
            scheme: https
    EOF
    
  2. 创建 IngressExtension 资源以处理从 contoso.com/summer-promotioncontoso.com/shop/category/5 的基于路径的重定向。

    kubectl apply -f - <<EOF
    apiVersion: alb.networking.azure.io/v1
    kind: IngressExtension
    metadata:
      name: summer-promotion-redirect
      namespace: test-infra
    spec:
      rules:
        - host: contoso.com
          requestRedirect:
            statusCode: 302
            path:
              type: ReplaceFullPath
              replaceFullPath: /shop/category/5
    EOF
    

部署必要的入口资源

  1. 创建第一个入口资源以侦听 HTTPS 请求。

    kubectl apply -f - <<EOF
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-https
      namespace: test-infra
      annotations:
        alb.networking.azure.io/alb-namespace: alb-test-infra
        alb.networking.azure.io/alb-name: alb-test
        alb.networking.azure.io/alb-frontend: ingress-fe
    spec:
      ingressClassName: azure-alb-external
      tls:
        - hosts:
            - contoso.com
          secretName: listener-tls-secret
      rules:
        - host: contoso.com
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: echo
                    port:
                      number: 80
    EOF
    
  2. 创建第二个入口资源以侦听端口 80 并重定向到 HTTPS。

    kubectl apply -f - <<EOF
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-http
      namespace: test-infra
      annotations:
        alb.networking.azure.io/alb-namespace: alb-test-infra
        alb.networking.azure.io/alb-name: alb-test
        alb.networking.azure.io/alb-frontend: ingress-fe
        alb.networking.azure.io/alb-ingress-extension: http-to-https
    spec:
      ingressClassName: azure-alb-external
      rules:
        - host: contoso.com
          http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: echo
                    port:
                      number: 80
    EOF
    
  3. 创建第三个入口资源以侦听端口 80 和 443,以获取 contoso.com/summer-promotion 并重定向到 contoso.com/shop/category/5

    kubectl apply -f - <<EOF
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-summer-promotion-redirect
      namespace: test-infra
      annotations:
        alb.networking.azure.io/alb-namespace: alb-test-infra
        alb.networking.azure.io/alb-name: alb-test
        alb.networking.azure.io/alb-frontend: ingress-fe
        alb.networking.azure.io/alb-ingress-extension: summer-promotion-redirect
    spec:
      ingressClassName: azure-alb-external
      tls:
        - hosts:
            - contoso.com
          secretName: listener-tls-secret
      rules:
        - host: contoso.com
          http:
            paths:
              - path: /summer-promotion
                pathType: Prefix
                backend:
                  service:
                    name: ignored-for-redirect
                    port:
                      number: 80
    EOF
    

注意

当 ALB 控制器在 ARM 中创建适用于容器的应用程序网关时,它将对前端资源使用以下命名约定:fe-<8 个随机生成的字符>

如果要更改在 Azure 中创建的前端的名称,请考虑按照创建自己的部署策略中的说明进行操作。

对于每个入口资源,请确保状态有效,已对侦听器编程,并已将地址分配给入口资源。 对于全部三个入口资源,应在此示例中看到相同的主机名。

kubectl get ingress ingress-https -n test-infra -o yaml

成功创建了入口时的示例输出。

status:
  loadBalancer:
    ingress:
    - hostname: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.fzyy.alb.azure.com
      ports:
      - port: 443
        protocol: TCP

测试对应用程序的访问

现在,我们已准备好通过分配给前端的 FQDN 将一些流量发送到示例应用程序。 使用以下命令获取 FQDN。

fqdn=$(kubectl get ingress ingress-http -n test-infra -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')

如果使用 curl 命令指定了服务器名称指示器,则 http://contoso.com 应当返回来自适用于容器的应用程序网关的响应,其中包含一个 ___location 标头,定义到 https://contoso.com 的 301 重定向。

fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:80:$fqdnIp http://contoso.com/ -v

通过响应,应会看到:

* Added contoso.com:80:xxx.xxx.xxx.xxx to DNS cache
* Hostname contoso.com was found in DNS cache
*   Trying xxx.xxx.xxx.xxx:80...
* Connected to contoso.com (xxx.xxx.xxx.xxx) port 80 (#0)
> GET / HTTP/1.1
> Host: contoso.com
> User-Agent: curl/7.81.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< ___location: https://contoso.com/
< date: Mon, 26 Feb 2024 22:56:23 GMT
< server: Microsoft-Azure-Application-LB/AGC
< content-length: 0
<
* Connection #0 to host contoso.com left intact

如果使用 curl 命令指定了服务器名称指示器,则 https://contoso.com/summer-promotion 适用于容器的应用程序网关应返回到 https://contoso.com/shop/category/5 的 302 重定向。

fqdnIp=$(dig +short $fqdn)
curl -k --resolve contoso.com:443:$fqdnIp https://contoso.com/summer-promotion -v

通过响应,应会看到:

> GET /summer-promotion HTTP/2
> Host: contoso.com
> user-agent: curl/7.81.0
> accept: */*
>
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* TLSv1.2 (IN), TLS header, Supplemental data (23):
* TLSv1.2 (OUT), TLS header, Supplemental data (23):
* TLSv1.2 (IN), TLS header, Supplemental data (23):
< HTTP/2 302
< ___location: https://contoso.com/shop/category/5
< date: Mon, 26 Feb 2024 22:58:43 GMT
< server: Microsoft-Azure-Application-LB/AGC
<
* Connection #0 to host contoso.com left intact

恭喜,你已安装 ALB 控制器、部署了后端应用程序,并使用入口 API 配置了 HTTP 到 HTTP 的重定向以及到特定客户端请求的基于路径的重定向。