0

I'm having a hard time trying to figure out how to achieve the routing/rewriting rules I need for the application I'm trying to set up and would be very grateful for any assistance.

I have a simple web app running on three Cloud Compute VM Instance Groups. The app is running fine on each VM (single VM per instance group). The app is identical apart from a small detail in the HTML which identifies the instance.

The Load Balancer is working fine in terms of routing traffic to the instances as such using paths, but I need to provide each user of the app a unique URL. What I would really like to achieve is something like this:

Url Path Routes to Instance Group
/bronze/summary /summary 1
/silver/summary /summary 2
/gold/summary /summary 3

This way each user hits their own URL path and the LB routes the request to /summary on the specific instance group which is running that user's version of the app.

I cannot see how to set this up using the basic host and path rule view. Using the advanced host and path rule view seems to offer more possibilities and I found this resource: https://cloud.google.com/load-balancing/docs/https/setting-up-url-rewrite

But, I'm not sure how to write the rules. What does not help from the example given in the link is that the target used is storage not instance groups (VMs) and I have no idea how to translate that example.

My current paths and rules file looks like this:

defaultService: projects/<project-name>/regions/europe-west2/backendServices/backend-1
name: path-matcher-1
pathRules:
- paths:
  - /summary
  service: projects/<project-name>/regions/europe-west2/backendServices/backend-1
- paths:
  - /bronze/summary
  service: projects/<project-name>/regions/europe-west2/backendServices/backend-1
- paths:
  - /silver/summary
  service: projects/<project-name>/regions/europe-west2/backendServices/backend-2
- paths:
  - /gold/summary
  service: projects/<project-name>/regions/europe-west2/backendServices/backend-3

But I cannot see how to remove the '/bronze' segment in the path prior to routing. I think if I had a route on the web app like '/bronze/summary' then it would work, but that would be a giant pain to implement.

Can someone guide me in the right direction, please?

THANKS!!

UPDATE

After a lot of messing about and struggling with the documentation, I came up with this:

defaultService: projects/<project-name>/regions/europe-west2/backendServices/backend-1
name: path-matcher-1
pathRules:
- paths:
  - /bronze/*
  service: projects/<project-name>/regions/europe-west2/backendServices/backend-1
  routeAction:
    urlRewrite:
      pathPrefixRewrite: /
- paths:
  - /silver/*
  service: projects/<project-name>regions/europe-west2/backendServices/backend-2
  routeAction:
    urlRewrite:
      pathPrefixRewrite: /
- paths:
  - /gold/*
  service: projects/<project-name>/regions/europe-west2/backendServices/backend-3
  routeAction:
    urlRewrite:
      pathPrefixRewrite: /

It seems to do what I need.

All I do now, is clone down the template app and as part of the local build and run (on the VM itself), I'm updating the Navigation partial to prefix with '/bronze/', '/silver/' or '/gold/' and it all seems to be working fine.

Got to say the documentation on this is quite hard going for a newbie, could do with a lot more simple examples.

Hopefully this may be useful to someone.

Indrid
  • 962
  • 4
  • 25
  • 39

1 Answers1

0

I do not believe you can do this. The load balancer rewriting doesn't have the capacity to rewrite the URL to remove something in the middle. I had to do almost this exact thing (except with Cloud Run targets instead of Compute Engine) and what I did was set up an HAProxy inside another Cloud Run instance (also behind the load balancer) that rewrote the URL, set a header, and then sent the request back to the load balancer. I configured the load balancer to send requests without the header to HAProxy, and otherwise route the request with the rewritten URL based upon the header.

There may be a better way, but that's what I came up with and it seems to work quite well. I had to use a GCP load balancer because I was using Cloud Run instances as the backend (via serverless NEGs), but if you are using a GCE backend presumably you also could just use a different load balancer.

Matt Wilbert
  • 376
  • 1
  • 8
  • Thanks Matt. Appreciate the response. I've updated my question with something that seems to be working well, but smarter folks than me may be able to shoot holes in it. – Indrid Jan 21 '23 at 17:44
  • Since you don't need the information from the path (in my case I did), that looks good to me. I should have realized you didn't need to capture the /summary part. Glad you've solved your problem! – Matt Wilbert Jan 22 '23 at 00:03