Virtual Hosting of Buckets

Virtual Hosting, in general, is the practice of serving multiple web sites from a single web server. One way to differentiate sites is by using the apparent host name of the request instead of just the path name part of the URI. An ordinary Amazon S3 REST request specifies a bucket using the first slash delimited component of the Request-URI path. Alternatively, using Amazon S3 Virtual Hosting, you can address a bucket in a REST API call using the HTTP Host header. In practice, Amazon S3's interpretation of Host means that most buckets are automatically accessible (for limited types of requests) at http://bucketname.s3.amazonaws.com. Furthermore, by naming your bucket after your registered domain name and by making that name a DNS alias for Amazon S3, you can completely customize the URL of your Amazon S3 resources, for example: http://my.bucketname.com/

Besides the attractiveness of customized URLs, a second benefit of virtual hosting is the ability to publish to the 'root directory' of your bucket's virtual server. This can be important because many existing applications search for files in this standard location. For example, favicon.ico, robots.txt, crossdomain.xml, are all expected to be found at the root.

So long as your GET request does not use the SSL endpoint, you may specify the bucket for the request using the HTTP Host header. The Host header in a REST request is interpreted as follows:

The following example illustrates these cases:

Example URLExample RequestBucket Name for RequestKey for RequestNotes
http://s3.amazonaws.com/
johnsmith/homepage.html
GET /johnsmith/homepage.html HTTP/1.1
Host: s3.amazonaws.com
johnsmithhomepage.htmlThe ordinary method
 
GET /johnsmith/homepage.html HTTP/1.0
johnsmithhomepage.htmlHTTP 1.0 may omit the Host header
http://johnsmith.
s3.amazonaws.com/homepage.html
GET /homepage.html HTTP/1.1
Host: johnsmith.s3.amazonaws.com
johnsmithhomepage.htmlAll lower-case Amazon S3 buckets are automatically addressable by the sub-domain method.
 
GET /homepage.html HTTP/1.1
Host: JohnSmith.s3.amazonaws.com
johnsmithhomepage.htmlNote that the case of the Host header is insignificant. Upper-case bucket names are not accessible using this method.
http://www.johnsmith.net/
homepage.html
GET /homepage.html HTTP/1.1
Host: www.johnsmith.net
www.johnsmith.nethomepage.htmlTo host a website in Amazon S3 using this method, you must configure your DNS name as a CNAME alias for bucketname.s3.amazonaws.com.

Depending on your needs, you might not want "s3.amazonaws.com" to appear on your web site or service. For example, if you host your web site's images on Amazon S3, you may prefer a URL like this:

http://images.johnsmith.net/

to one that looks like this:

http://johnsmith-images.s3.amazonaws.com/

To associate a hostname with an Amazon S3 bucket using CNAMEs

  1. Select a hostname that belongs to a domain you control. This example uses the images subdomain of the johnsmith.net domain.

  2. Create a bucket that matches the hostname. In this example, the hostname and bucket are named images.johnsmith.net.

    [Note]Note

    Your bucket name must exactly match the hostname.

  3. Create a CNAME record that defines the hostname as an alias for the Amazon S3 bucket. For example:

    images.johnsmith.net CNAME images.johnsmith.net.s3.amazonaws.com

    [Important]Important

    For request routing reasons, the CNAME record must be defined exactly as above. Otherwise, it might appear to operate correctly, but will eventually result in unpredictable behavior.

    [Note]Note

    The exact procedure for configuring DNS depends on your DNS server or DNS provider and is beyond scope of this document.

Because DNS names are case insensitive, only lower-case buckets are addressable using the virtual hosting method.

Specifying the bucket for the request using the HTTP Host header is supported:

Early versions of Amazon S3 incorrectly ignored the HTTP Host header. Applications that depend on this undocumented behavior must be updated to set the Host header correctly. Because Amazon S3 determines the bucket name from Host when present, the most likely symptom of this problem is to receive an unexpected NoSuchBucket error result code.