Sign Up

Tutorials

Getting Started

This tutorial will guide you through the process of creating Fastly account and setting up your first service. Follow the steps below...

Step 1 - Sign Up

Go to https://app.fastly.com/#signup and fill in your information.

Step 2 - Check Your E-mail

Once you sign up you will be sent an e-mail with a confirmation link. Click the link to continue.

Step 3 - Setup Your Service

Once you've confirmed your account you will be logged-in into the Fastly App. You will need to provide a few pieces of information to setup your service:

  • Domain - Your site's domain name (ex: www.example.com).
  • Server IP - The IP address for your site's server.
  • Server Port - The Port for your site's server (usually 80).

Step 4 - CNAME Your Domain

Finally you must CNAME your domain to a.prod.fastly.net, once you've done that you should be all ready to go!

If you are having any problems feel free to contact us at support@fastly.com.

Cache Control

You are in full control of how Fastly caches your resources. First prefered way of instructing Fastly is to use backend HTTP headers. The other way is to use Varnish Configuration Language (VCL). If you'd like to use VCL you will need to contact our support team so that it can be enabled on your account.

Please be reminded that you can use longer expirations coupled with our purging API to make your site faster and your backend less loaded. Expirations in following examples are provided as guidance only.

Backend HTTP headers

You can set 4 different types of HTTP headers which will have different effect on our caches and web browsers. If you use more than one type, they have priority in the order they're listed below.

Surrogate-Control
Format:
Surrogate-Control: max-age=(time in seconds)
e.g.
Surrogate-Control: max-age=3600
will cache something on our caches for one hour. This header gets stripped and is only visible to Fastly Caches
Cache-Control: s-maxage
Format
Cache-Control: s-maxage=(time in seconds)

Is same as Surrogate-Control except the header is not stripped and will be listened to by our servers, and any caches between us and the browser

Cache-Control: max-age
Format
Cache-Control: max-age=(time in seconds)
Will be listened to by all of the above and the browser
Expires

Caches content until it expires as specified Please read section 14.21 of RFC2616 for explanation of the format

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Will be listened to by all of the above and the browser

Do not cache

If you don't want certain resource cached set
max-age=0
or set the Expires to current time or in the past.

More complicated examples

Q: I want Fastly to cache it forever but send headers to the browsers so that they don't cache it at all (so that every browser miss hits Fastly but isn't a cache miss from your service)

The best way to do this would be to send us both the Cache-Control header as you want it to go to the browsers, and use Surrogate-Control to tell us how long to cache for. So for instance:

    Cache-Control: max-age=3600
    Surrogate-Control: max-age=2592000

The Surrogate-Control header takes priority over Cache-Control, and unlike Cache-Control it is stripped so the browsers don't see it either.

Example backend configs

Apache Config

If you are using Apache easiest way to add headers is to use the mod_expires module.

http://httpd.apache.org/docs/2.0/mod/mod_expires.html

For example to cache GIF images for 75 minutes since image was accessed by the cache server you would add a directive like this under the VirtualHost (or globally)

    ExpiresActive On
    ExpiresByType image/gif "access plus 1 hours 15 minutes" 

You can also protect whole URL subtrees e.g.

    <Location "/css">	
      ExpiresActive On
      ExpiresDefault "access plus 1 year"
    </Location>
    
    <Location "/static/">
      ExpiresActive On
      ExpiresDefault "access plus 1 day"
    </Location>
NGINX configuration

To configure NGINX. Add the expires directive e.g.

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
       expires 1h;
    }

Alternatively if you need more flexibility in modifying headers you can try the HttpHeadersMore Module

http://wiki.nginx.org/HttpHeadersMoreModule

Amazon S3 configuration

By default S3 doesn't set any Cache Control headers so you will have to do this file by file by using the S3Explorer or in automated fashion by using a cloud library like boto. For example following web page

http://blackpawn.com/blog/?p=391

does a good job of describing how to set Cache Control headers on all your JPG and PNG documents in all your buckets. In their example they are setting data cache control headers to cache data for 10 days. Please remember that you can combine long cache time with instant purges to enhance your performance.

Also note that example below is an example for customers with small to medium size buckets. It iterates over every object in your bucket(s) so if you have millions of objects this may not be the right approach. In that case we would recommend using Varnish Configuration Language (VCL). Please contact us for assistance.

    from boto.s3.connection import S3Connection
    
    connection = S3Connection('aws access key', 'aws secret key')
    
    buckets = connection.get_all_buckets()
    
    for bucket in buckets:
        for key in bucket.list():
            print('%s' % key)	
    
            if key.name.endswith('.jpg'):
                contentType = 'image/jpeg'
            elif key.name.endswith('.png'):
                contentType = 'image/png'
            else:
                continue
    
            key.metadata.update({
                'Content-Type': contentType,
                'Cache-Control': 'max-age=864000'
            })
            key.copy(
                key.bucket.name,
                key.name,
                key.metadata,
                preserve_acl=True
            )
    

Custom headers in programming languages/frameworks

PHP

http://php.net/manual/en/function.header.php

e.g. add this to your PHP code before you send any output to cache certain HTML for an hour

    header('Cache-Control: max-age=3600');
Django
Please refer to

https://docs.djangoproject.com/en/dev/ref/request-response/#setting-headers

e.g.

    response = HttpResponse()
    response['Cache-Control'] = 'max-age=3600'
Sinatra
Please refer to

http://sinatra.rubyforge.org/doc/

e.g.

    get '/' do
        headers['Cache-Control'] = 'max-age=3600'
    end