使用负载均衡器和 CDN 在 Google Cloud Storage 上部署 SPA 应用程序

问题描述 投票:0回答:2

我正在研究一种使用 GCS、负载均衡器和 CDN 在 Google Cloud 上部署 Angular 或 React Web 应用程序的方法。

我已经使用 urlRewrite 设置了 LB 和 GCS,但是由于 LB 仅允许 pathPrefixRewrite 不允许 full URL rewrite,所以我无法将所有请求重定向到

/index.html

我想实现与 Firebase 托管相同的功能,您可以在其中指定重写规则

"rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]

另一个选项是将 404 页面设置为

index.html
,但这会导致服务器返回
404 status code
,这是我不喜欢的。

负载均衡器可以实现这一点吗,因为它们不支持完全重写?

reactjs angular google-cloud-platform google-cloud-storage load-balancing
2个回答
1
投票

Google 在 2020 年添加了重写引擎(请参阅https://serverfault.com/questions/927143/how-to-manage-url-rewrites-with-a-gcp-load-balancer/1045214)并且它只能

pathPrefixRewrite
(剥离固定前缀)。

对于 SPA,您需要将变量后缀重写为

index.html
,以便您使用 Nginx 或 Firebase。

或者提到存储桶的 HTTP 服务器中的错误处理程序(虚拟 URL 的 HTTP 代码 404 的缺点):

gsutil web set -m index.html -e index.html gs://web-stage/

设置 SPA 应用程序的完整说明如下:Google 云负载均衡器为 Firebase 托管创建后端服务,以通过负载均衡器为来自同一域的微服务和前端 SPA 提供服务?

LB重写引擎限制讨论:


0
投票

现在可以使用全局外部负载均衡器路由规则重写每个云存储请求来服务index.html。

defaultService: projects/xxxx/global/backendBuckets/dashboard
name: path-matcher-1
routeRules:
- matchRules:
  - pathTemplateMatch: /**
  priority: 10
  service: projects/xxxx/global/backendBuckets/dashboard
  routeAction:
    urlRewrite:
      pathTemplateRewrite: /index.html
© www.soinside.com 2019 - 2024. All rights reserved.