Can multiple domains be linked to individual directories within a single S3 bucket on Fastly?


#1

I want to host many websites from inside individual directories of a single S3 bucket and have them linked to three different domains on Fastly like so:

site-1.com  =>  s3-us-west-2.amazonaws.com/my-bucket/site-1/index.html
site-2.com  =>  s3-us-west-2.amazonaws.com/my-bucket/site-2/index.html
site-3.com  =>  s3-us-west-2.amazonaws.com/my-bucket/site-3/index.html
...
site-n.com  =>  some-bucket/site-n/index.html

I can do this using Cloudfront because its distributions support an additional path option which can route the request to the correct sub-directory inside the bucket. I don’t see a way to do this on Fastly and want to ask if anyone knows how this can be done?


#2

@roman this is possible. For each URL path that you would want to rewrite, set up a new header rule like the following:

Then, with each new header rule, add a conditional like this:

What this does is do a swap of your bucket’s root directory to /path-you-define/ and then make sure that it only happens if the proper inbound host header is set.


#3

@aspires can this be done through the APIs and therefore via the Fastly gem?


#4

Yes. Everything in the Fastly app is build API-first, so you can use API calls to make these changes.


#5

@aspires thanks I’ve been trying it but I’m having trouble setting the header regexp field. For some reason it is always ignored when I set it via the APIs:

...

condition = fastly_client.create_condition(
  :service_id => service.id,
  :version => latest_version_number,
  :name => 'host name condition',
  :priority => 10,
  :type => 'request',
  :statement => %Q(req.http.Host == "bucket-name.s3.amazonaws.com")
)

...

header = fastly_client.create_header(
  :service_id => service.id, 
  :version => latest_version.number, 
  :name => 'path-on-s3-bucket', 
  :ignore_if_set => false, 
  :type => 'request', 
  :dst => 'url',
  :src => 'req.url',
  :action => 'regex', 
  :regexp => '^/',
  :substitution => '/sites/path/version/',
  :request_condition => condition.name
)

The above produces this result: dl.dropboxusercontent.com/u/816353/Screen%20Shot%202015-03-24%20at%2012.04.44%20pm.png

Sorry I can’t share photo links


#6

@aspires never mind I figured out where things were going wrong:

The attribute name is not regexp it was regex. I was looking at lib/fastly/header.rb which confused the hell out of me.


#7

Hi.

I’m building a static site generator for a custom CMS.

We have potentially hundreds of clients/projects per client that need a custom domain name.
eg: client-domain.com

We intend to use a single s3 bucket to host all the sites using sub-directories since s3 has a hard limit on buckets.
static_sites_bucket/client_domain.com/
static_sites_bucket/other_client.net/

I’ve been testing fastly vcl rules a way do setup this type of behaviour without much success (I get 404s when accessing)

But I have other question as well.
Does fastly provide a dedicated IP so that my clients can point their DNS CNAME’s www.client_domain.com to fastly

And then I would create a rule to redirect www.client_domain.com to the respective s3 bucket directory.

Is this scalable to hundreds of domains pointing to a single backend?

Thanks.


#8

It may make sense to file a support ticket so we can take a deeper look at your VCL for this.

Yes, we do. But it’s a little more complex. If you mention it in that same email to discuss your VCL we can go over it.