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

How to point my load balancer domain to index.html while hosting in a private gcs bucket

Hi All,

The objective is that I am in need of hosting a static website in a Private GCS bucket. On top of the bucket I'll use load balancer with Cloud Armor policies to restrict access.

I followed this blog https://medium.com/@thetechbytes/private-gcs-bucket-access-through-google-cloud-cdn-430d940ebad9

Created a similar setup in the project. The setup is as given below.

  • A private gcs bucket with the static files
  • A network endpoint group pointing to the GCS buckets domain
  • A Backend service with cloud CDN enabled pointing to the NEG created before
  • Added the HMAC key and secret to the Backend service as mentioned in the blog.
  • Created an external load balancer with the created backend service and added TLS certificate for our own domain

The issue is if I access the URL with it shows loads an XML format page with the list of objects inside the bucket. Only if I mention the path /index.html in the URL it loads to that page. I want to know how to write a LB routing rule to achieve the below.

If someone uses URL example.com it should automatically load as example.com/index.html

1 4 2,539
4 REPLIES 4

 Hi @AnbuKLB ,

Can you try creating a URL map & then a URL path matcher as described in 

https://cloud.google.com/load-balancing/docs/https/ext-load-balancer-backend-buckets#create_an_with_... . Specifically, please see the examples in the gcloud tab of that page, as shown in this screenshot:
 
kumards_0-1695998374167.png

For the --backend-bucket-path-rules=PATH=BUCKET argument, replace PATH with /*, as shown in the following example:

--backend-bucket-path-rules=/*=mybucket

Hi @kumards 

I have not used backend buckets here. I only have private GCS bucket. I can't make it public. And so I didn't go with Backend buckets. I created a NEG and mapped everything as mentioned in the post. If backend buckets can be private please let me know I'll also try that setup. But the current setup is working for me. The only thing is I want to acheive the below.

If I enter the site name it should automatically redirects example.com -> example.com/index.html

Replying here in case it helps someone in the future. I had the same issue and I was able to solve it by inserting a pathRule with a urlRedirect, which redirects from the path `/` to the pathRedirect `/index.html`.

GCP examples for traffic management

GCP documentation for URL Maps.

Here is an example in Terraform Code how I did my URL Map:

 

 

resource "google_compute_url_map" "cdn_lb" {
  name            = "cdn-lb"
  description     = "Load Balancer to redirect requests to bucket backend"
  default_service = google_compute_backend_service.cdn_backend_service.id

  host_rule {
    hosts        = ["*"]
    path_matcher = "matcher1"
  }

  path_matcher {
    name = "matcher1"
    default_service = google_compute_backend_service.cdn_backend_service.id

    path_rule {
      paths   = ["/"]
      url_redirect {
        path_redirect = "/index.html"
        strip_query = true
      }
    }
  }
}

 

Is your issue resolved I am facing the same issue can you guide what steps you followed to map it to index.html