8

We are going to use Google Cloud Load Balancer with React.js application. React.js application has special routing rules. We are going to organize our application structure in the following way.

-[BUCKET]uiresources
  -[FILE]index.html
  -[FOLDER]dist
    -[FILE]src.bundle.js
    -[FOLDER]...
    -[FILE]

Also we need to route api calls to GCF endpoints. That means that they should be proxied to another domain, that, seems to be impossible because we configuring backend-service with VM as a target.

Example of routing

  1. / ---------------------------> /index.html (To storage bucket)
  2. /index.html --------------> /index.html (To storage bucket)
  3. /signup -------------------> /index.html (To storage bucket)
  4. /someroute --------------> /index.html (Still to storage bucket)
  5. /api/signup ---------------> anotherhost.com/signup (To GCF endpoint with long url)
  6. /resources/images -----> /resources/images (To storage bucket)

We can say that in another way, like, if our route contain dot, that will mean that we requesting file, then return appropriate file, otherwise always return index.html.

It is possible to use subdomains if it will help to build such navigation. For example:

Another routing example

So, the question is just how? I setup load balancer using tutorial and tried to configure the rules here, but I didn't have any luck with it. I started from index.html issue and this is what I have at the moment.

enter image description here

Previously I've managed this on Azure and on simple nginx server, but on this two platforms more powerful routes configurations is available. I don't know if it possible here, but hope someone can help me with that.

All the VM instances are running under Debian 8 with Apache server. I though about routing rules inside each VM instance, but for me it looks a little bit crazy now and something that will bring a lot of troubles in the future.

Artsiom Miksiuk
  • 3,896
  • 9
  • 33
  • 49

1 Answers1

10

URL maps

The URL routing you're configuring is referred to as a URL map in GCE HTTP/HTTPS load balancer configuration.

Capabilities and restrictions

I would recommend you read about URL maps to understand how it works. To be specific, you will need to understand the capabilities and the restrictions of URL maps in load balancing.

This list is not comprehensive, but more suited to your specific use case:

  • You can add rules based on host names and paths. Host matches the host name for the request. For each host, you specify a path matcher (which is a collection of different path strings) to specify the paths to match (Eg. /foo/*, /bar/*, /foo/bar/baz/*). These are exactly the text boxes you see in the configuration UI. Paths column in the UI corresponds to the path matcher.

  • You can set up different rules for the same path string as long as they are for different hosts.

  • If no host is specified, it will match all hosts.

  • There is always a default path matcher /* per host, which will decide how to handle requests which were not matched by any of the defined rules.

  • The available destinations for routing the requests are backend services and backend buckets.

  • You will use a backend service to route the requests to a VM (actually an instance group - which is a collection of VMs).

  • You will use a backend bucket to route the requests to content stored in Google Cloud Storage buckets.

  • When routing requests from a Load balancer to a backend service, your VM will get the request containing the full requested URI. So, the service on your VM can decide how to handle it by looking at the path.

  • When routing requests from a Load balancer to a backend bucket, the full path in the request (from /) should match the path of the object stored in your Google Cloud Storage Bucket. Eg. if your request is for a path https://www.example.org/foo/bar/baz/info.txt which gets routed to a backend bucket, your corresponding GCS bucket should have a file at this location /foo/bar/baz/info.txt.

URL map configuration for your use case

Having said that, it is possible to map all the requests as you specified with some minor modifications and taking advantage of HTTP 301 (permanent URL redirects) using a backend service.

/ -------------------> /index.html
/index.html ---------> /index.html (in GCS bucket)
/signup -------------> /index.html (in GCS bucket)
/someroute ----------> /index.html (in GCS bucket)
/api/signup ---------> anotherhost.com/signup
/resources/images/* -> /resources/images/* (in GCS bucket)
/* ------------------> Recommend using a backend service which returns a 404.

You will need to configure your URL map as follows:

/ -------------------> Backend service which returns a HTTP 301 (permanent URL redirection) /index.html
/index.html ---------> Backend bucket (will take to /index.html in the GCS bucket)
/signup -------------> Backend service which returns a HTTP 301 (permanent URL redirection) /index.html (which will cause the user to hit your GCS bucket)
/someroute ----------> Backend service which returns a HTTP 301 /index.html (will redirect to the /index.html content from your GCS bucket)
/api/signup ---------> Backend Service which returns a HTTP 301 (permanent URL redirection) to anotherhost.com/signup
/resources/images/* -> Backend bucket - Will pull /resources/images/* in GCS bucket
/* ------------------> Backend Service which returns a 404. 
Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • Thank you, for your answer, but it is unclear, how to setup redirects? Where this can be configured? On each VM instance or there is another settings for load balancer? – Artsiom Miksiuk Jun 27 '17 at 04:25
  • 1
    GCE load balancer does not let you configure redirects. You will have to do this on your VM in your application which receives the request. – Tuxdude Jun 27 '17 at 04:59
  • and how I can centralize redirect configuration? I mean, if I will need to change something I will need to change it on all VMs, and what if my infrastructure will consist from 10 VMs or 100s VMs? I can use VM templating, but it still not solves the issue with configs distribution across all VM instances. – Artsiom Miksiuk Jun 27 '17 at 05:12
  • Everything that I was able to find, that we can create managed instances group. When change is required, all instances should be replaced with the new instances with updated configs. Is it correct? – Artsiom Miksiuk Jun 27 '17 at 05:26
  • 1
    You can use [Managed instance groups](https://cloud.google.com/compute/docs/instance-groups/#managed_instance_groups) in your backend service. Managed instance groups are built using an [Instance template](https://cloud.google.com/compute/docs/instance-groups/#instance_templates). It also supports [autoscaling](https://cloud.google.com/compute/docs/autoscaler/) if needed. – Tuxdude Jun 27 '17 at 05:27
  • 1
    Yes, you're right. You can update all instances in the managed instance group by updating the template. Do note that updating the template, only affects any new instances that will be created in the instance group. Existing instances which are running using an older template will not be affected, unless they are recreated. [Reference](https://cloud.google.com/compute/docs/reference/latest/instanceGroupManagers/setInstanceTemplate) – Tuxdude Jun 27 '17 at 05:32
  • @Tuxdude You mentioned that the file structure and content of the bucket should match with the full path of the request. Is it possible to set the the path to point to the root (/) path of the bucket instead? Ex. example.com/auth -> maps to the / of the bucket instead of the /auth – Jplus2 Jul 29 '22 at 07:24