lusk
lusk

Reputation: 562

Custom 301 redirects for static site using GCP Storage

I am considering whether to host my static website on an AWS S3 bucket or using Google Cloud Platform Storage, and, as I already use GCP and with it being generally cheaper, I would really like to utilize that option.

My issue is that I often need to create custom 301 redirects for my site, like:

https://example.com/page -> https://anotherexample.com/another-page

S3 seems to handle this well, but I'm not finding any documentation on custom redirects from GCP.

Is this possible with GCP Storage buckets yet?

Upvotes: 4

Views: 4644

Answers (5)

dparizek
dparizek

Reputation: 100

You have to use a Cloud Load Balancer in front of your Google Cloud Storage static site in order to setup redirects.

You likely want to do this anyway, as it is necessary to serve https content (as opposed to http).

You can add as many redirects as you want but beware a gotcha - they charge for them. As of today, pricing is:

For first 5 forwarding rules: $0.025/hour Each additional forwarding rule: $0.01/hour (https://cloud.google.com/vpc/network-pricing#lb)

That adds up. Like for example, after the first 5 rules, your 6th rule costs you a mere penny per hour. But a penny per hour = $87.60 per year. So imagine you want 100 redirects... oh my.

Upvotes: 5

buckaroo1177125
buckaroo1177125

Reputation: 1683

While Google Cloud Storage still does not support redirects, the GCP HTTP load balancer now supports it.

Upvotes: 2

Kangaroo
Kangaroo

Reputation: 31

I am managing an 8500 page static website in GCS. I managed this using Cloudflare.com . It has a preprocessor called "Workers" which use javascript.

You can either have Key-Value Pairs or an Array within the code for 301 redirects.

Upvotes: 1

dsesto
dsesto

Reputation: 8178

As pointed out by @Scalar, currently GCP does not support redirects with static website hosting in Google Cloud Storage, so just to add some more detailed information on the proposal to serving content as a backend service, let me share with you some documentation guidelines that might be helpful for you.

Currently, a Cloud Storage bucket can be served as a backend service (in fact a backend bucket) in order for your requests for static content made against a content-based load balancing system to be served by that bucket (while the rest of the requests are handled by your instances; although you can skip that part, as you only need the Cloud Storage service in your configuration).

In order to set up a backend bucket to your load balancer, you can follow the steps detailed in the following documentation page. It assumes that you have previously completed the creation of a content-based load balancer, so you can start from that example. Then, you can set up redirections so that calls to https://example.com/page are redirected to https://anotherexample.com/another-page, and they are finally processed by your load balancing service, which will direct them to your bucket. Redirects can be performed at the application level by means of the web server of your choice, but just to give you a couple of examples, you can do that using NGINX's return or rewrite directives, as explained in their official documentation, or also Apache Server's Redirect or Rewrite rules, as detailed in their documentation to.

Upvotes: 1

Scalar
Scalar

Reputation: 225

Short answer: GCP does not seem to support such a feature.

However there is a hard way to do this in GCP:

You can setup an instance group of micro virtual machines with Nginx, configured to make redirects for you.

Then you'll need to setup a load balancer to handle all requests. It supports forwarding rules, so you may configure it to send https://example.com/page requests to nginx VM's, and all other requests to Storage Buckets.

Upvotes: 1

Related Questions