google-cloud-platformroutesload-balancingurl-routinggcp-load-balancer

GCP load balancer url re-writing and backend-mapping not happening the way I'd expect


I am using a classic application load balancer with two backends. I want my main backend to handle most requests and my auth backend to handle requests to /auth/*. I've been playing around with different configurations for my url-map but haven't been able to get it to work. What am I doing wrong?

Request url Expected BE Expected Output Path Actual BE Actual Ouput Path
https://example.com my-main-be https://example.com my-auth-be ''
https://example.com/ my-main-be https://example.com/ my-auth-be ''
https://example.com/unknown my-main-be https://example.com/unknown my-auth-be ''
https://example.com/auth/handler my-auth-be https://example.com/handler my-auth-be https://example.com/auth/handler

Here is my url-map configuration including tests:

defaultService: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/my-main-be
hostRules:
- hosts: [example.com, '*.example.com']
  pathMatcher: path-matcher-1
name: my-urlmap
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/my-main-be
  name: path-matcher-1
  routeRules:
  - matchRules:
    - pathTemplateMatch: /auth/{path=**}
    priority: 1
    service: https://www.googleapis.com/compute/v1/projects/my-project/global/backendBuckets/my-auth-be
    routeAction:
      urlRewrite:
        pathTemplateRewrite: /{path}

tests:
- description: Empty path routs to my-main-be
  host: example.com
  path: https://example.com
  service: projects/my-project/global/backendServices/my-main-be
  expectedOutputUrl: https://example.com

- description: / routs to my-main-be
  host: example.com
  path: https://example.com/
  service: projects/my-project/global/backendServices/my-main-be
  expectedOutputUrl: https://example.com/

- description: /unknown routs to my-main-be
  host: example.com
  path: https://example.com/unknown
  service: projects/my-project/global/backendServices/my-main-be
  expectedOutputUrl: http://example.com/unknown

- description: /auth/handler routs to my-auth-be
  host: example.com
  path: https://example.com/auth/handler
  service: projects/my-project/global/backendBuckets/my-auth-be
  expectedOutputUrl: https://example.com/handler

And here is the output when I try to validate it:

gcloud compute url-maps validate --source ./my-map --load-balancing-scheme=EXTERNAL --global
result:
  loadSucceeded: true
  testFailures:
  - actualOutputUrl: ''
    actualRedirectResponseCode: 0
    actualService: BACKEND_BUCKET/<ID>.my-auth-be
    expectedOutputUrl: https://example.com/
    expectedRedirectResponseCode: 0
    expectedService: BACKEND_SERVICE/<ID>.my-main-be
    host: example.com
    path: https://example.com
  - actualOutputUrl: ''
    actualRedirectResponseCode: 0
    actualService: BACKEND_BUCKET/<ID>.my-auth-be
    expectedOutputUrl: https://example.com/
    expectedRedirectResponseCode: 0
    expectedService: BACKEND_SERVICE/<ID>.my-main-be
    host: example.com
    path: https://example.com/
  - actualOutputUrl: ''
    actualRedirectResponseCode: 0
    actualService: BACKEND_BUCKET/<ID>.my-auth-be
    expectedOutputUrl: http://example.com/unknown
    expectedRedirectResponseCode: 0
    expectedService: BACKEND_SERVICE/<ID>.my-main-be
    host: example.com
    path: https://example.com/unknown
  - actualOutputUrl: http://example.com/auth/handler
    actualRedirectResponseCode: 0
    actualService: BACKEND_BUCKET/<ID>.my-auth-be
    expectedOutputUrl: https://example.com/handler
    expectedRedirectResponseCode: 0
    expectedService: BACKEND_BUCKET/<ID>.my-auth-be
    host: example.com
    path: https://example.com//auth/handler
  testPassed: false

Solution

  • Chat GPT gave me an alternative configuration that fixed things:

    routeRules:
    - priority: 1
      matchRules:
      - prefixMatch: /auth/
      service: https://www.googleapis.com/compute/v1/projects/my-project/global/backendBuckets/my-auth-be
      routeAction:
        urlRewrite:
          pathPrefixRewrite: /