具有多个后端的Google云负载均衡器Http-to-https

具有多个后端的Google云负载均衡器Http-to-https

我的目标是为 Google Cloud Platform 上的 3 个实例组的负载均衡器实现 http-to-https 重定向。

我有一个 http-to-https 负载均衡器,它会将我的 3 个应用程序(3 个后端服务、3 个实例组)重定向到另一个负载均衡器。我遵循文档,它只对其中 1 个 banckend 服务有效,即默认服务。当我 curl 后端时,其他 2 个后端不会重定向到 https

负载均衡器 http 到 https 设置

实例组负载均衡器
此处未显示,但两个负载均衡器中的 IP 都匹配

当我卷曲https://domain1.fr -> OK
当我卷曲时http://domain1.fr -> 301 重定向至 https -> 确定

当我卷曲https://domain2.fr -> OK
当我卷曲时http://domain2.fr -> 正常(但没有重定向到 https)

当我卷曲https://domain3.fr -> OK
当我卷曲时http://domain3.fr -> 正常(但没有重定向到 https)

这里未显示,但如果我在 LoadBalancer 中将默认后端更改为 backend2,它会重定向到 domain2.fr 的 https。

答案1

您必须添加额外的主机和路径规则,以根据特定域将 http 重定向到 https。这可以在 Google Cloud 控制台 UI 中或使用 gcloud sdk 完成。

  1. 在 UI 中,您可以编辑 LB 并添加“新主机和路径规则”,并将“主机”和“主机重定向”设置为您的域。对每个域执行此操作。

  2. 使用 gcloud,您可以导出 URL 映射,按照如下所示修改内容并上传。

a. 导出现有的 URL 映射 -

gcloud compute url-maps export YOUR-URL-MAP --destination=/tmp/urlmap.yaml

对于单域重定向 web1.example.net,原始 yaml 文件将如下所示 -

name: webmap
kind: compute#urlMap
defaultUrlRedirect:
  hostRedirect: web1.example.net
  httpsRedirect: true
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  stripQuery: false

添加要重定向的第二个域 - 在本例中为 web2.example.net。注意hostRulesandpathMatchers部分。您可以为每个域添加额外的主机规则和 pathMatcher -

name: webmap
kind: compute#urlMap
defaultUrlRedirect:
  hostRedirect: web1.example.net
  httpsRedirect: true
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  stripQuery: false
hostRules:
- hosts:
  - web2.example.net
  pathMatcher: path-matcher-1
pathMatchers:
- defaultUrlRedirect:
    hostRedirect: web2.example.net
    httpsRedirect: true
    redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
    stripQuery: false
  name: path-matcher-1

您可以导入此文件 -

gcloud compute url-maps import YOUR-URL-MAP --source=/tmp/urlmap.yaml

更改需要几分钟才能生效。

相关内容