配置 Google 负载均衡器主机和路径规则以使用 GCF 正确服务 react.js 应用程序

Configure Google Load Balancer host and path rules to serve react.js application properly with GCF

我们将在 React.js 应用程序中使用 Google 云负载均衡器。 React.js 应用程序有特殊的路由规则。我们将按以下方式组织我们的应用程序结构。

-[BUCKET]uiresources
  -[FILE]index.html
  -[FOLDER]dist
    -[FILE]src.bundle.js
    -[FOLDER]...
    -[FILE]

我们还需要将 api 调用路由到 GCF 端点。这意味着它们应该被代理到另一个域,这似乎是不可能的,因为我们将后端服务配置为 VM 作为目标。

路由示例

  1. / --------------------------> /index.html (到存储桶)
  2. /index.html --------------> /index.html (到存储桶)
  3. /signup ------------------> /index.html (到存储桶)
  4. /someroute ----------------> /index.html (还在存储桶)
  5. /api/signup --------------> anotherhost.com/signup (To GCF endpoint with long url)
  6. /resources/images -----> /resources/images (到存储桶)

我们可以换句话说,如果我们的路由包含点,那将意味着我们请求文件,然后 return 适当的文件,否则总是 return index.html .

如果有助于构建此类导航,则可以使用子域。例如:

另一个路由示例

所以,问题是如何?我使用教程设置负载均衡器并尝试在此处配置规则,但我没有任何运气。我从 index.html issue 开始,这就是我现在所拥有的。

以前我在 Azure 和简单的 nginx 服务器上管理过这个,但是在这两个平台上可以使用更强大的路由配置。我不知道这里是否可行,但希望有人能帮助我。

所有 VM 实例都是 运行 在 Debian 8 和 Apache 服务器下。我虽然考虑了每个 VM 实例内部的路由规则,但对我来说它现在看起来有点疯狂而且将来会带来很多麻烦。

URL 地图

您正在配置的 URL 路由在 GCE HTTP/HTTPS 负载均衡器配置中称为 URL map

能力和限制

我会推荐你​​ read about URL maps to understand how it works。具体来说,您需要了解 URL 映射在负载平衡中的功能和限制。

此列表并不全面,但更适合您的特定用例:

  • 您可以添加基于主机名和路径的规则。 Host 匹配请求的主机名。对于每个主机,您指定一个路径匹配器(它是不同路径字符串的集合)以指定要匹配的路径(例如 /foo/*/bar/*/foo/bar/baz/*)。这些正是您在配置 UI 中看到的文本框。 UI中的Paths列对应路径匹配器。

  • 同一个路径字符串可以设置不同的规则,只要是针对不同的主机即可。

  • 如果没有指定主机,则匹配所有主机。

  • 每个主机总是有一个默认路径匹配器/*,它将决定如何处理不符合任何定义规则的请求。

  • 路由请求的可用目的地是 backend services and backend buckets

  • 您将使用 backend service 将请求路由到 VM(实际上是一个实例组 - 这是 VM 的集合)。

  • 您将使用后端存储桶将请求路由到存储在 Google Cloud Storage buckets 中的内容。

  • 当将请求从负载均衡器路由到 backend service 时,您的 VM 将获得包含完整请求 URI 的请求。因此,您 VM 上的服务可以通过查看路径来决定如何处理它。

  • 当将请求从负载均衡器路由到 backend bucket, the full path in the request (from /) should match the path of the object stored in your Google Cloud Storage Bucket 时。例如。如果您的请求是针对路由到后端存储桶的路径 https://www.example.org/foo/bar/baz/info.txt,则相应的 GCS 存储桶应该在此位置 /foo/bar/baz/info.txt.

  • 有一个文件

URL 适用于您的用例的地图配置

话虽如此,可以通过一些小的修改并利用 HTTP 301 (permanent URL redirects) 使用后端服务来映射您指定的所有请求。

/ -------------------> /index.html
/index.html ---------> /index.html (in GCS bucket)
/signup -------------> /index.html (in GCS bucket)
/someroute ----------> /index.html (in GCS bucket)
/api/signup ---------> anotherhost.com/signup
/resources/images/* -> /resources/images/* (in GCS bucket)
/* ------------------> Recommend using a backend service which returns a 404.

您需要按如下方式配置 URL 地图:

/ -------------------> Backend service which returns a HTTP 301 (permanent URL redirection) /index.html
/index.html ---------> Backend bucket (will take to /index.html in the GCS bucket)
/signup -------------> Backend service which returns a HTTP 301 (permanent URL redirection) /index.html (which will cause the user to hit your GCS bucket)
/someroute ----------> Backend service which returns a HTTP 301 /index.html (will redirect to the /index.html content from your GCS bucket)
/api/signup ---------> Backend Service which returns a HTTP 301 (permanent URL redirection) to anotherhost.com/signup
/resources/images/* -> Backend bucket - Will pull /resources/images/* in GCS bucket
/* ------------------> Backend Service which returns a 404.