27
votes

Is there any way to host an entire site on Google Cloud Storage? That is, map a bucket directly to a top level domain.

E.g. create a bucket called mysite.com, then when the user visits mysite.com serve the content directly from cloud storage?

I see how to CNAME a subdomain, e.g. static.mysite.com, but can't figure out how to map the top-level domain to a bucket.

Any solution?

8

8 Answers

21
votes

I wanted to do this too. But there is currently no way of binding Google Cloud Storage buckets directly to a top-level domain name, as you found out.

The IP addresses where Google Cloud Storage answers web requests change often (to adapt to changing conditions on the Internet), so Google doesn't want to you add a subset of those (which would quickly get out of date) as A records for the DNS server of your website. Google instead requires a CNAME to a DNS name it controls, so it can change the IP addresses for Google Cloud Storage as often as it needs to. And a CNAME can only be added to a subdomain and not a top-level domain. (This is because when there is a CNAME at a given level, the DNS server for that domain will not answer any other queries for that level. But a top-level domain needs to answer also at least NS and SOA queries.)

The only workaround for this right now is, as described in another answer, to set up the CNAME for the www subdomain, and configure your DNS hosting service to do a http-level redirect from the top-level domain to the www subdomain. (Mosty providers will support that.)

If you're not a fan of this solution (like me), there are exactly three possibilities. The first one is to wait for Google to offer the possibility to host top-level domains on Google Cloud Storage. That requires Google to DNS hosting, first though. Enabling top-level domain support would require extra work on Google's part (after building their DNS hosting offering), but it's only possible if Google controls both parts. I have no clue if or when Google will ever offer that, though. But one can hope.

The second solution is to switch to another big provider. Amazon can host static websites from S3 on a top-level domain. But that's because they also host DNS for that domain. There may be others. All large cloud providers will serve data from a set of frequently-changing IP addresses. So if they don't host your DNS too, they will need you to set a CNAME on a subdomain.

The third (theoretical, at least) solution is to switch to a smaller cloud provider, offering equivalent service, maybe on OpenStack, etc. Because they're smaller, their IP routing setup will be quite a bit simpler than the big players (Amazon, Google, etc.) and they would then be able to give you a small set of IP addresses to set as A records on your top-level domain. This is just an educated guess on my part, though. I don't know right now of any such smaller cloud storage providers.

(Well, okay, the fourth possibility is to give up on the idea of hosting your website statically. But personally, I really like this option when possible, at least from a security and responsiveness point of view.)

11
votes

This is an old question, but in case anyone comes across this post- I encountered the same issue today.

Google Domains is my domain registrar. Assuming this is also the case for you, here's how you would set it up:

Under "Custom resource records"...

Create a CNAME record for "www" that points to "c.storage.googleapis.com".

Under "Synthetic records"...

Choose "Subdomain forward". Enter "mydomain.com" in the left-most (subdomain) input and "www.mydomain.com" in the right-most (destination URL) input.

2
votes

It's been a few years, but to anyone facing the same issue (mapping a "naked domain" to a GCS bucket), I see a couple of ways of achieving this.

One is to use any DNS provider that supports "CNAME flattening" (aka ALIAS, or ANAME records). Examples are: DNSimple, NS1, Dyn, DNS Made Easy, and Cloudflare.

Cloudflare is also one of the recommended ways of serving your bucket through HTTPS, and might also save you some money on your egress bill.

Another option, is to use this project to serve your files through App Engine. You get SSL for free, can use it on your "naked domain", and it allows you to configure redirects, rewrites, and custom headers.

1
votes

An alternate solution is to use CloudFlare (free), they allow support CNAME on the Zone Apex / Naked Domain / Root Domain. It works fine for my static site on Google Cloud Storage.

This still leaves the issue of the www ....

The solution that works for me is using CloudFlare is 1. set both www and the naked domain CNAME to c.storage.googleapis.com 2. set a 'Page Rule' for example.com/* to 301 to http://www.example.com/$1

This now means my whole site is on GCloud Storage ( ignore my self comment below as that was before I found 'Page Rules' )

0
votes

@Christian Hudon's answer is very clear. I've been trying to do the same and I've been having the problems discussed above. What you can also do is the following:

Create two CNAME entries one empty (example.com) and another one WWW (www.example.com) Now create two bucket folders: example.com and www.example.com Decide which one will host the pages and assets and which one just the pages.

Let's say www.example.com is the source so you have your folder structure, images, css, javascripts, etc... in there and because the site is static what you can do is to add the full domain on the source of your assets.

Something like

 <link rel="stylesheet" href="http://www.example.com/css/main.css" type="text/css" />

Then copy over the html pages only to example.com. Obviously if you have example.com/contact/index.html you need to create the folder too.

It's a little bit annoying but it was the only way I could get it to work. This is not ideal if you need to change the content quite often but at least it works, it all depends on your needs.

0
votes

One way you can do this is by creating a Google Cloud CDN pointing to the bucket (example.com).

The CDN's load balancer should have a static IP address that you can use as an A record for @.

0
votes

You can't host your root domain from google cloud storage as of now, due to the cname restriction in DNS. You will have to have alias record in DNS pointing to an ipaddress to serve the root domain.

Firebase seems to be an alternate ideal option for this and is also almost free for standard traffic. Also has very few steps to host a static site - installing firebase cli with npm and a couple of commands to deploy. You can then get a list of ips from the firebase console that you can connect to your domain, by adding them in a alias record in your DNS.

https://firebase.google.com/docs/hosting

-2
votes

Yes, this is possible and is documented here: https://developers.google.com/storage/docs/website-configuration

Basically, you need to set up a CNAME redirect to c.storage.googleapis.com