cookiesgoogle-cloud-platformload-balancinggoogle-cloud-url-maps

GCP url-map with cookie regex matching rule


I am setting up a GCP url map to route requests to backend services based on cookie values. Since cookies would have multiple key values, I am trying to use a regex matcher. I need to route requests to backends based on region value from cookie. A typical cookie would look like this: foo=bar;region=eu;variant=beta;

defaultService: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-1
kind: compute#urlMap
name: regex-url-map
hostRules:
- hosts:
  - '*'
  pathMatcher: path-matcher-1
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-1
  name: path-matcher-1
  routeRules:
    - matchRules:
        - prefixMatch: /
          headerMatches:
            - headerName: Cookie
              regexMatch: (region=us)
      priority: 0
      service: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-1
    - matchRules:
        - prefixMatch: /
          headerMatches:
            - headerName: Cookie
              regexMatch: (region=eu)
      priority: 1
      service: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-2

However, this url-map fails validation with this error:

$ gcloud compute url-maps validate --source regex-url-map.yaml
result:
  loadErrors:
  - HttpHeaderMatch has no predicates specified
  loadSucceeded: false
  testPassed: false

Please note that an exact match with cookie passes validation and matches correctly if cookie value is just something like this: region=us. The headerMatches section for exact match would look like this:

          headerMatches:
            - headerName: Cookie
              exactMatch: region=us

Any pointers on what am I doing wrong here? Thanks!


Solution

  • Your way of reasoning is correct but the feature you're trying to use is unsupported in external load balancing in GCP; it works only with internal load balancing.

    Look at the last phrase from the documentation:

    Note that regexMatch only applies to Loadbalancers that have their loadBalancingScheme set to INTERNAL_SELF_MANAGED.

    enter image description here

    I know it isn't the answer you're looking for but you can always file a new feature request on Google's IssueTracker and explain in detail what you want, how it could work etc.

    You can always try to pass the region value in the http request - instead of requesting https://myhost.com all the time - also if you could add a suffix, for example: https://myhost.com/region1 it would allow the GCP load balancer rules to process it and direct the traffic to the backend you wish.

    Have a look at this example what you can and can't do with forwarding rules in GCP. Another example here. And another one (mine) explaining how to use pathMatcher to direct traffic to different backend services.