Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Configure routing of backends in global load balancer

I have a client service built on Nextjs 13 and an express server, both of them are deployed on cloud run using artificat registry, with the ingress set to internal only and allow external load balancer.

I am using HTTP(s) global external load balancer, and I have set up a frontend service for the load balancer with a peramanent IP. In the backend I have added 2 services, the client and the server both connected with different Serverless NEGs. 

Independently both the services are working fine, but when I configure the routing in the load balancer, its only one of the service which is rendered in the browser. For example:

I have set the routes client as the default, `/*` to go for the client and `/json-api/*` to go to the server backend.  When I run `<ip-address>` it goes to the client Next.js app which is expected. But when I go to `<ip-address>/json-api/` It stills stays in the client app and doesn't redirect to the express server, which it was supposed to do.

I have tried doing the reverse, where server is the default and `/table` goes to the client and `/json-api` goes to the express server. It is still the same behaviour where `/table` instead of showing the Next.js page, shows the default page of Express server saying `Cannot GET /table`

Also to note, I have added the same IP address as the host for both the routing paths.

1 2 1,350
2 REPLIES 2

Hi @dev-shetty ,

In your case, you want to route requests to the client application for the root path (/*) and to the Express server for requests under /json-api/*. Based on this, you can configure your path rules as follows:

Order of Path Rules:

  • Rule 1: Path: /json-api/*, Backend: Express Server
  • Rule 2: Path: /*, Backend: Next.js Client

Make sure that Rule 1 comes before Rule 2 in the configuration so that requests to /json-api/* are matched by Rule 1, and other requests are matched by Rule 2.

Path Matching:

  • Rule 1: /json-api/*
  • Rule 2: /*

These path patterns are straightforward. Requests matching /json-api/* will be directed to the Express server, and all other requests will go to the Next.js client.

Here is an example config:

Path Rule 1:
Path: /json-api/*
Backend: Your Express Server Backend Service

Path Rule 2:
Path: /*
Backend: Your Next.js Client Backend Service

I also recommend to check your backend service configurations, health checks, and any other relevant settings to be sure that everything is correctly set up.

Hope this helps.

Thanks for the help Marvin,

Actually I was trying to route with only the IP address and not a Fully Qualified Domain Name

In one of the docs by GCP https://cloud.google.com/load-balancing/docs/url-map#gcloud_1 , it is mentioned that routing doesn't work with only IP address. I guess most probably this was the issue, I didn't try adding domain name yet, but will update the result it once I try it.