You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Application Gateway与APIM路径路由配置问题排查求助

问题分析与解决方案

一、当前配置的核心问题

1. Application Gateway重写规则的潜在风险

若AGW的重写规则是直接替换路径而非添加前缀,会导致原始请求的路径被覆盖,比如把/api/v1/xxx直接改成/staging/api/*(而非/staging/api/v1/xxx),这会让APIM收到的路径不符合预期,触发404。

2. APIM策略的关键缺陷

  • 健康探测未适配:AGW对APIM的健康探测请求(默认是GET /)不含/staging//qa前缀,会走到APIM的默认逻辑,Basic层无匹配API时返回404,导致AGW判定后端不健康。
  • 路径替换逻辑不精准:用Replace("/staging", "")会替换路径中所有出现的/staging,比如/api/v1/staging/test会被错误改成/api/v1/test,应该只替换开头的前缀。
  • 缺少QA分支处理:当前策略仅覆盖Staging请求,QA路径的请求无匹配规则,直接返回404。

二、修正后的配置方案

1. Application Gateway重写规则确认

确保重写规则为前缀添加逻辑:

  • Staging监听器:将原始路径/api/v1/**重写为/staging/api/v1/**
  • QA监听器:将原始路径/api/v1/**重写为/qa/api/v1/**

注意:重写规则需设置为"在现有路径前添加前缀",避免覆盖原始路径的业务部分。

2. 修正后的APIM入站策略

<inbound>
    <base />
    <!-- 优先处理AGW健康探测请求,直接返回200 -->
    <choose>
        <when condition="@(context.Request.OriginalUrl.Path == "/" || context.Request.OriginalUrl.Path.StartsWith("/health"))">
            <return-response>
                <set-status code="200" reason="OK" />
                <set-body>OK</set-body>
            </return-response>
        </when>
        <!-- 处理Staging前缀请求,仅替换开头的/staging -->
        <when condition="@(context.Request.OriginalUrl.Path.StartsWith("/staging/"))">
            <rewrite-uri template="@(Regex.Replace(context.Request.OriginalUrl.Path, "^/staging", ""))" />
            <set-backend-service backend-id="staging-api-public-loadbalancer" />
            <set-header name="Host" exists-action="override">
                <value>staging.xyz.com</value>
            </set-header>
        </when>
        <!-- 处理QA前缀请求,仅替换开头的/qa -->
        <when condition="@(context.Request.OriginalUrl.Path.StartsWith("/qa/"))">
            <rewrite-uri template="@(Regex.Replace(context.Request.OriginalUrl.Path, "^/qa", ""))" />
            <set-backend-service backend-id="qa-api-public-loadbalancer" /> <!-- 替换为你的QA后端ID -->
            <set-header name="Host" exists-action="override">
                <value>qa.abc.com</value>
            </set-header>
        </when>
        <!-- 未匹配的请求返回404 -->
        <otherwise>
            <return-response>
                <set-status code="404" reason="Not Found" />
            </return-response>
        </otherwise>
    </choose>
</inbound>

3. AGW健康探测配置调整

  • 将APIM后端池的健康探测路径改为/health(与APIM策略中匹配的路径一致)
  • 确保探测方法为GET,预期状态码包含200

三、验证步骤

  1. 检查AGW后端池状态:确认所有APIM实例显示为"健康"
  2. 测试Staging请求:访问https://你的AGW域名/api/v1/xxx,验证请求被正确转发到staging.xyz.com/api/v1/xxx并返回正常响应
  3. 测试QA请求:同理访问QA路径,验证响应正常

内容的提问来源于stack exchange,提问作者userahd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:07:19