Azure APIM重写url以没有后缀url

标签 azure azure-resource-manager azure-api-management

我在 APIM 中有一个包含不同操作的 API。 其中一项操作称为例如测试操作

testOp 将发送给它的请求转发到 azure 中的函数应用 (FA)。 我决定将其写入名为 fa-url 的命名值中,而不是在策略中显式写入 FA URL。所以 API 策略如下所示:

<policies>
    <inbound>
        <base />
        <set-backend-service base-url="{{fa-url}}" />
        <rewrite-uri template="/" />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

此命名值是通过 ARM 模板创建的,该模板将此命名值的值设置为 FA 的触发 URL。这是ARM代码:

 {
      "properties": {
        "tags": [],
        "secret": true,
        "displayName": "fa-url",
        "value": "[listsecrets(resourceId(parameters('faRG'), 'Microsoft.Web/sites/functions', parameters('functionAppName'), parameters('functionName')), variables('apiFuncApp')).trigger_url]"
      },
      "name": "[concat(parameters('ApimServiceName'), '/fa-url')]",
      "type": "Microsoft.ApiManagement/service/namedValues",
      "apiVersion": "2021-01-01-preview"
    },

此 Arm 模板和命名值将生成一个类似于以下内容的 URL:

https://fa-name.xx.companything.net/api/realFunctionname?code=123456=

因此,该 url 以查询参数结尾,该参数以等号结尾。 现在的问题是:这个 API 操作 (testOp) 在其定义中有一个 URL

testOp 看起来与此类似:

enter image description here 在另一张图片中: enter image description here

因此,当我尝试通过此操作向 FA 发送请求时,收到 404 Not Found401 Unauthorized 错误。 经过检查跟踪,原因基本上是因为重写 URL 在 fa URL 末尾添加了“/”。所以不要发送到:

https://fa-name.xx.companything.net/api/realFunctionname?code=123456=

它发送到

https://fa-name.xx.companything.net/api/realFunctionname?code=123456=/

如果我删除了重写 URL 功能,我仍然会收到错误,因为现在它将发送到:

https://fa-name.xx.companything.net/api/realFunctionname?code=123456=/testOp

有什么方法可以防止在后端 URL 中添加任何后缀吗?或者您有其他建议来解决这个问题吗?

注意:我还尝试将重写 URL 设置为空字符串,但当我这样做时,azure 拒绝保存策略。

最佳答案

好吧,看来我唯一需要做的就是使用这个重写策略:

<rewrite-uri template="?" />

这将使 URL 看起来完全符合我的要求,即:

https://fa-name.xx.companything.net/api/realFunctionname?code=123456=

关于Azure APIM重写url以没有后缀url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73830789/

相关文章:

VNET 中的 Azure 容器实例如何获取 FQDN

Azure列出所有实例REST API未在响应中返回提到的值

Azure 应用程序 map 未连接 Application Insight

azure - DocumentDB数据结构误解

powershell - 将虚拟文件夹添加到 Azure Web App

azure - 如何使用 ARM 模板通过应用程序见解启用实时 Azure Web 应用程序分析

azure - 如何在 Bicep 中填充 Azure SignalR 服务的上游代码

azure - 使用 MSAL 库 java 获取 Azure 应用服务主体

Azure API 管理 : Using OAuth to secure a connection between the gateway and a backend?

azure - 使用 NuGet 安装 Windows Azure Storage v3.0.3 失败