首页 > 解决方案 > 使用 Firebase 托管托管 Google Cloud Run 会引发混合内容错误

问题描述

我有一个 Python(带有 uvicorn 的 FastAPI)应用程序在 Google Cloud Run(完全托管版本)的 Docker 容器中运行。我还有一个在 Firebase 上运行的单独的网络应用程序,它使用 Firebase 托管。我想使用 Firebase 托管将 Cloud Run 容器与 Firebase 应用集成。我按照https://firebase.google.com/docs/hosting/cloud-run中的说明设置了托管配置。

"rewrites": [
  {
    "source": "/api/**",
    "run": {
      "serviceId": "myapp",
      "region": "us-central1"
    }
  },
  {
    "source": "**",
    "destination": "/index.html"
  }
]

我的 Firebase 应用使用以下代码调用 API

const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({payload  : payload, temperature: temperature})
    }

fetch('/api/generate', requestOptions)...

但是,当我尝试这样做时,我从浏览器控制台收到以下错误:

Mixed Content: The page at 'https://mydomain.web.app/' was loaded over HTTPS, but requested an insecure resource 'http://myapp-somerandomhash-uc.a.run.app/api/generate'. This request has been blocked; the content must be served over HTTPS.

(假设 Firebase 应用托管在https://mydomain.web.app上,而 Cloud Run 应用托管在https://myapp-somerandomhash-uc.a.run.app上)

当前行为:

  1. Firebase 应用调用https://mydomain.web.app/api/generate
  2. Firebase托管将其重定向到http://myapp-somerandomhash-uc.a.run.app/api/generate
  3. (我收到上述错误)
  4. Cloud Run 容器应用再次将其重定向到https://myapp-somerandomhash-uc.a.run.app/api/generate

期望的行为:

  1. Firebase 应用调用https://mydomain.web.app/api/generate
  2. 无需显式重定向或重定向到 https 版本即可获得响应

来自 Cloud Run 的日志:

INFO: 169.254.8.129:21990 - "POST /api/generate HTTP/1.1" 307 Temporary Redirect
POST 307 314 B 8ms Chrome 83 https://myapp-somerandomhash-uc.a.run.app/api/generate

有没有关于如何解决这个问题的建议?


标签: firebasefirebase-hostinggoogle-cloud-run

解决方案


我设法解决了我的问题。我的开发环境对斜杠不敏感,所以我错过了 .../api/generate/ 和 ../api/generate 之间的不匹配。因此,在修复了这种不匹配之后,一切都按预期工作。

我不完全确定我的堆栈的哪一部分导致了上述错误,但至少对于 Cloud Run (FastAPI + uvicorn) 和 Firebase Hosting 尾部斜杠似乎很重要。


推荐阅读