Search code examples
google-cloud-platformurl-rewritinggoogle-cloud-rungoogle-cloud-load-balancer

Is it possible to rewrite an incoming request to the backend service (e.g. Cloud Run) from a GCP load balancer?


I have two services running Cloud Run such as api1 and api2.

I set up a global external HTTP(S) load balancer (classic) in GCP to route the path to both services.

such as

Paths Action Backend
/api1/* Route traffic to a single backend service-api1
/api2/* Route traffic to a single backend service-api2

When I send a request mydomain/api1/aaa/ccc/, api1 get the request url as mydomain/api1/aaa/ccc/

When I send a request mydomain/api2/ddd/eee/, api2 get the request url as mydomain/api2/ddd/eee/

Is there a way to rewrite the incoming request for api1, so that api1 will see the request as mydomain/aaa/ccc/?

This also apply to api2, so api2 will see the request as mydomain/ddd/eee/

From the Rewrite documentation,

The load balancer provides the following mutually exclusive primary actions:

  • Reads the incoming URL in the request.
  • Replaces the host, the path, or both the host and the path, transforming the URL before directing traffic to the backend service or backend bucket.

I think there is a way, but I don't manage to make it work.

Any advice?


Solution

  • In load balancing console page

    1. Select Advanced host and path rule (URL redirect, URL rewrite) in Host and path rules

    enter image description here

    1. Add a path rule and edit path rule as below (show Add-On action)

    enter image description here

    1. Save and update the changes.

    When you send a request mydomain/api1/aaa/ccc/, api1 will see the request as mydomain/aaa/ccc/.