如何将两个 Cloud 运行 应用程序置于一个域下以避免 CORS

How to bring two Cloud Run Apps under one domain to avoid CORS

我想通过 Cloud 运行 拥有两个应用程序 "fully managed"。一个是纯 Vue.js SPA,另一个是属于它的后端服务器,连接到 MySQL 并且还获取其他一些 API 端点。

现在我已经部署了这两个应用程序,但完全不知道如何让前端应用程序访问后端应用程序。它们应该 运行 在同一个域中以避免前端。

前端应用的当前 URL:https://myapp-xl23p3zuiq-ew.a.run.app

所以我希望可以通过以下方式访问服务器:https://myapp-xl23p3zuiq-ew.a.run.app/api

使用 Cloud 运行 可以实现吗?

目前这在 Cloud 运行 中是不可能的,正如在对您的问题的评论中所说的那样。

您可以通过将请求类型从 Bug 更改为功能请求来检查 Buganizer (Google Issue Tracker), currently there seems to be none, and if that is indeed the case, you can create a new Feature Request 上是否有此功能的任何功能请求,并且随着 Google 在他们的路线图上开发它,您将通知。

希望对您有所帮助。

我遇到了同样的问题。人们通常拥有的一般想法是使用路径映射并将 / 映射到您的客户端,将 / 服务器映射到您的后端。谷歌搜索了一段时间后,我发现了这个:

https://cloud.google.com/run/docs/mapping-custom-domains

Base path mapping: not supported

The term base path refers to the URL path name that is after the domain name. For example, users is the base path of example.com/users. Cloud Run only allows you to map a domain to /, not to a specific base path. So any path routing has to be handled by using a router inside the service's container or by using Firebase Hosting.

选项 1:

我最终创建了一个 "all in one" docker 图像,其中 nginx 作为反向代理,客户端(一些静态文件)和服务器(在我的例子中是 python 应用程序由uwsgi).

如果您正在寻找灵感,可以在此处查看 public 存储库:https://gitlab.com/psono/psono-combo

选项 2:

另一种方法是在 client.example.com 上托管您的客户端,在 server.example.com 上托管您的服务器,然后在 example.com 下使用反向代理创建第三个 docker 运行 实例。 所有请求都将 "proxied" 到客户端和服务器。您的用户只会与 example.com 交互,因此 CORS 不会成为问题。

选项 3:

配置 CORS,这样访问 example.com 的人也可以连接到 server.example.com