【问题标题】:Azure Functions proxy returns 404 on localhost onlyAzure Functions 代理仅在 localhost 上返回 404
【发布时间】:2021-10-07 17:25:55
【问题描述】:

我遇到的问题似乎与 this one 相同,但解决方案不起作用 - proxies.json 曾经并且设置为“始终复制”。

我有带有 Azure Functions v2 和 docker 容器的 .net 核心 API,我想向其代理一些请求。部署后一切正常,但本地代理返回 404 响应。

proxies.json:

{
  "$schema": "http://json.schemastore.org/proxies",
  "proxies": {
    "review": {
      "debug": true,
      "matchCondition": {
        "route": "api/{docId}/proxy/review/{*restOfPath}"
      },
      "backendUri": "https://my-docker-container.azurewebsites.net/{restOfPath}",
      "requestOverrides": {
        "backend.request.headers.Some-Header": "request.headers.Cookie"
      }
    }
  }
}

本地 docker 容器正在侦听端口 3000,因此我将 backendUri 替换为 http://localhost:3000/{restOfPath},但它返回 404。Docker 端点永远不会被命中。我可以通过从 Postman 调用预期的 URL 来确认 docker 容器正在运行和工作。

除了解决方案之外,还有什么方法可以尝试更多地调试它吗?无法从日志中获取任何有用的信息。

使用已部署资源时的成功日志:

[2021-08-02 10:01:06] Executing HTTP request: {
[2021-08-02 10:01:06]   "requestId": "8c60239b-046d-464b-8a3a-09e0f5c55090",
[2021-08-02 10:01:06]   "method": "POST",
[2021-08-02 10:01:06]   "uri": "/api/4708a861-d8b2-4033-a1da-1a07afca4161/proxy/review/api/review"
[2021-08-02 10:01:06] }
[2021-08-02 10:01:06] Function started (Id=ebfbabc0-1a5b-4129-9280-14ae322e23ba)
[2021-08-02 10:01:06] Executing 'Functions.review' (Reason='This function was programmatically called via the host APIs.', Id=ebfbabc0-1a5b-4129-9280-14ae322e23ba)
[2021-08-02 10:01:06] Executing request via Azure Function Proxies
[2021-08-02 10:01:22] Function completed (Success, Id=ebfbabc0-1a5b-4129-9280-14ae322e23ba, Duration=16363ms)
[2021-08-02 10:01:22] Executed 'Functions.review' (Succeeded, Id=ebfbabc0-1a5b-4129-9280-14ae322e23ba)
[2021-08-02 10:01:22] Executed HTTP request: {
[2021-08-02 10:01:22]   "requestId": "8c60239b-046d-464b-8a3a-09e0f5c55090",
[2021-08-02 10:01:22]   "method": "POST",
[2021-08-02 10:01:22]   "uri": "/api/4708a861-d8b2-4033-a1da-1a07afca4161/proxy/review/api/review",
[2021-08-02 10:01:22]   "authorizationLevel": "Anonymous",
[2021-08-02 10:01:22]   "status": "OK"
[2021-08-02 10:01:22] }

使用本地资源不成功的日志:

[2021-08-02 07:58:02] Executing HTTP request: {
[2021-08-02 07:58:02]   "requestId": "f562eae9-b2c7-4ef8-84ba-83d42f59c4b7",
[2021-08-02 07:58:02]   "method": "POST",
[2021-08-02 07:58:02]   "uri": "/api/4708a861-d8b2-4033-a1da-1a07afca4161/proxy/review/api/review"
[2021-08-02 07:58:02] }
[2021-08-02 07:58:02] Function started (Id=28149473-125c-4fc4-958d-348604559e10)
[2021-08-02 07:58:02] Executing 'Functions.review' (Reason='This function was programmatically called via the host APIs.', Id=28149473-125c-4fc4-958d-348604559e10)
[2021-08-02 07:58:02] Executing request via Azure Function Proxies
[2021-08-02 07:58:02] Function completed (Success, Id=28149473-125c-4fc4-958d-348604559e10, Duration=169ms)
[2021-08-02 07:58:02] Executed 'Functions.review' (Succeeded, Id=28149473-125c-4fc4-958d-348604559e10)
[2021-08-02 07:58:02] Executed HTTP request: {
[2021-08-02 07:58:02]   "requestId": "f562eae9-b2c7-4ef8-84ba-83d42f59c4b7",
[2021-08-02 07:58:02]   "method": "POST",
[2021-08-02 07:58:02]   "uri": "/api/4708a861-d8b2-4033-a1da-1a07afca4161/proxy/review/api/review",
[2021-08-02 07:58:02]   "authorizationLevel": "Anonymous",
[2021-08-02 07:58:02]   "status": "NotFound"
[2021-08-02 07:58:02] }

更新: 如果有任何疑问可能与 Docker 有关,则可以将其划掉。创建了新的虚拟 API,这是同样的问题。

【问题讨论】:

    标签: azure proxy azure-functions


    【解决方案1】:

    找到了解决办法。 感谢https://markheath.net/post/azure-functions-v2-proxies

    需要在local.settings.json中添加"AZURE_FUNCTION_PROXY_DISABLE_LOCAL_CALL": true

    在此之后偶然发现了另一个问题 - backendUri 中的斜杠正在被编码,所以而不是例如http://localhost:3000/api/review 它正在调用 http://localhost:3000/api%2Freview。同样,只有本地问题。

    要解决这个问题,需要将"AZURE_FUNCTION_PROXY_BACKEND_URL_DECODE_SLASHES": true 添加到local.settings.json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-05
      • 2020-11-22
      • 2021-08-15
      • 2021-09-30
      • 2019-05-10
      • 2017-02-22
      • 1970-01-01
      • 2022-11-28
      相关资源
      最近更新 更多