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

Firebase Hosting搭配Functions重定向到错误URL该如何解决

问题原因

  1. 区域不匹配:你部署的函数位于europe-west3区域,但Firebase Hosting的rewrite规则默认会将请求转发到us-central1区域的函数,区域不匹配导致路由逻辑异常,最终出现路径重复拼接为/test/test的问题。
  2. 路径匹配规则覆盖不全:当前rewrite的source仅精确匹配/test路径,而Firebase Hosting默认会对无尾部斜杠的路径发起301重定向到带斜杠的/test/,重定向后的路径无法匹配现有规则,也会导致路由错误。
  3. 函数权限配置缺失:当前报错返回403权限错误,说明你的Cloud Function未配置公开访问权限,未认证用户无法调用。

解决建议

  • 调整firebase.json的rewrite配置,明确指定函数区域,同时优化路径匹配规则覆盖所有场景:
{
  "functions": {
    "source": "functions"
  },
  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [{
        "source": "/test{,/**}",
        "function": {
          "name": "test",
          "region": "europe-west3"
        }
    }],
    // 可选:关闭自动尾部斜杠重定向,避免不必要的跳转
    "trailingSlash": false
  }
}
  • 为函数配置公开访问权限,执行以下gcloud命令:
    gcloud functions add-iam-policy-binding test --region=europe-west3 --member="allUsers" --role="roles/cloudfunctions.invoker"
  • 配置修改完成后,重新部署生效:
    firebase deploy --only hosting,functions:test

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 03:06:02