firebase - 使用 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上)
当前行为:
- Firebase 应用调用https://mydomain.web.app/api/generate
- Firebase托管将其重定向到http://myapp-somerandomhash-uc.a.run.app/api/generate
- (我收到上述错误)
- Cloud Run 容器应用再次将其重定向到https://myapp-somerandomhash-uc.a.run.app/api/generate
期望的行为:
- Firebase 应用调用https://mydomain.web.app/api/generate
- 无需显式重定向或重定向到 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
有没有关于如何解决这个问题的建议?
解决方案
我设法解决了我的问题。我的开发环境对斜杠不敏感,所以我错过了 .../api/generate/ 和 ../api/generate 之间的不匹配。因此,在修复了这种不匹配之后,一切都按预期工作。
我不完全确定我的堆栈的哪一部分导致了上述错误,但至少对于 Cloud Run (FastAPI + uvicorn) 和 Firebase Hosting 尾部斜杠似乎很重要。
推荐阅读
- angular6 - Angular 6 - 如何通过单击图标转到另一个视图?
- java - 我在 eclipse Photon Release (4.8.0)、OS-win10、java 版本 10.0.2 上安装 TestNG 时遇到问题
- java - cal4j - 文件异常的意外结束
- python - 验证准确性在大数据集中只有几个值
- r - 如何将数据标题拆分到自己的列?
- javascript - 将 js var 传递给 solidity 函数
- node.js - 在被拒绝的承诺的捕获块中失败摩卡测试
- c - 斐波那契数列中的分段错误
- python - 按顺序返回项目序列的最快数据结构
- java - Java Streams -> 从列表中提取特定索引处的值
- >