首页 > 解决方案 > GCP url map 为不同的后端服务创建路径规则

问题描述

下面是我的地形脚本。我已经设置了必要的转发规则和目标 http 代理。后端服务也存在。我可以访问 下的所有路径/images/*,但是,我无法访问 下的路径/videos/*。从脚本来看,默认后端服务是backend-lb-prod路径所在的后端服务/images/*。我将默认后端服务切换为backend-lb,我现在可以访问/videos/*但不能访问/images/*

resource "google_compute_url_map" "url-map-be" {
  name            = "${var.platform_name}-url-map-be-prod"
  default_service = "${google_compute_backend_service.backend-lb-prod.self_link}"

  host_rule {
    hosts        = ["${var.backend_address_name}"]
    path_matcher = "allpaths"
  }

  path_matcher {
    name            = "allpaths"
    default_service = "${google_compute_backend_service.backend-lb-prod.self_link}"

    path_rule {
      paths   = [“/images"]
      service = "${google_compute_backend_service.backend-lb-prod.self_link}"
    }  

    path_rule {
      paths   = [“/images/*"]
      service = "${google_compute_backend_service.backend-lb-prod.self_link}"
    } 

    path_rule {
      paths   = ["/videos"]
      service = "${google_compute_backend_service.backend-lb.self_link}"
    }  

    path_rule {
      paths   = ["/videos/*"]
      service = "${google_compute_backend_service.backend-lb.self_link}"
    }

  }
}

当我在谷歌控制台上运行 url 映射的测试时,这是我得到的错误

Invalid value for field 'resource': ''. Test failure: Expect URL ‘*.*.*.*/videos/go' 
to map to service ‘***93-BACKEND_SERVICE-**-backend-lb', but actually mapped to 
'***93-BACKEND_SERVICE-**-backend-lb-prod’.

这就是google_compute_url_mapterraform文档中的说明方式。在这种情况下我错过了什么。

更新

我已经包含了/videos/images路径,上面的错误消失了。但是,从应用程序的日志来看,流量仍然是通过默认的后端服务发送的。

标签: google-cloud-platformgoogle-compute-engineload-balancingterraformgcloud

解决方案


希望这对其他人有帮助。最初host_rulehosts一个 IP 地址。我也将其更改为域名,但它仍将所有流量发送到默认后端服务。我将其更改为*现在可以按预期工作。还要注意的是,不是默认后端服务的路径需要几分钟才能访问。

host_rule {
  hosts        = ["*"]
  path_matcher = "allpaths"
}

推荐阅读