Help


Community tutorials and documentations

How to cache your Scaleway Scalable Infinite Storage (Scaleway SIS) objects with Varnish

How to cache your Scaleway Scalable Infinite Storage (Scaleway SIS) objects with Varnish

This page shows you how to use a C1 server to cache your Scaleway SIS objects with Varnish.

Requirements

  • You have an account and are logged into cloud.scaleway.com
  • Your SSH key is configured
  • You have an API Token
  • You have a running server
  • S3cmd is configured

In this guide, we will cache bootstrap. The procedure will also work for any other static website.

There are four steps to cache a static website with Scaleway SIS and Varnish

  • Create a new bucket
  • Upload your static website into the bucket
  • Configure Varnish
  • Test the Varnish configuration

Step 1 - Create a new bucket

In the Control Panel, click “Storage” from the left side menu.

You land on the Storage page that lists your buckets. Click on the “Create Bucket” button to create the bucket that will store our objects. Then, input a name for your bucket and validate.

Step 2 - Upload your static website files into the bucket

Now that your bucket is created, you need to upload your content inside. See how to configure S3cmd for Scaleway SIS to upload your files.

git clone https://github.com/twbs/bootstrap.git
cd bootstrap
git checkout gh-pages
s3cmd sync . s3://bootstrap --acl-public --add-header="Cache-Control: max-age=600"

The s3cmd command above does the following:

  • Upload bootstrap local directory to the bucket s3://bootstrap
  • Set the files inside the bucket public
  • Set the cache control header so that Varnish caches files during 600s before querying Scaleway SIS.

Step 3 - Configure Varnish

We now have to configure Varnish to work with Scaleway SIS.

First, connect to your C1 server via ssh ssh root@<instance_ip>

Then you have to install Varnish on your server. On Debian-like distributions, execute an apt-get update to update package lists and install Varnish with apt-get install varnish

Once Varnish installed, you have to configure it to cache the static pages stored into Scaleway SIS.

Open the /etc/varnish/default.vcl file, this file contains the caching policies.

For instance, to cache the files stored into the bootstrap bucket with Varnish the default.vcl file looks like this:

vcl 4.0;
    
backend default {
    .host = "bootstrap.fr-1.storage.online.net";
    .port = "http";
}
    
sub vcl_recv {
    set req.http.host = "bootstrap.fr-1.storage.online.net";
    set req.backend_hint = default;
}
    
sub vcl_backend_response {
  
    if (beresp.ttl == 0s) {
         set beresp.ttl = 0 s;
         set beresp.uncacheable = true;
     }

     else if (beresp.http.Cache-Control ~ "private") {
         set beresp.ttl = 0 s;
         set beresp.uncacheable = true;
     }
    return (deliver);
}

Save the file and restart the Varnish daemon service varnish restart.

By default, Varnish listens to the port 6081 and use 256m of memory to store your objects. Edit /etc/default/varnish to change the defaults.

DAEMON_OPTS="-a :6081 \
             -T localhost:6082 \
             -f /etc/Varnish/default.vcl \
             -S /etc/Varnish/secret \
             -s malloc,256m"

Step 4 - Test the Varnish configuration

Open your browser and access to http://server_ip:6081/index.html, your page should be displayed. You can disable the cache on any object by setting the Cache-Control header to private.

For instance if you do not want to cache the file.html you have to set the header as Cache-Control: private:

s3cmd put file.html s3://bootstrap/file.html --acl-public --add-header="Cache-Control: private"

Depending of the contents you want to serve, you might want to:

  • Cache your objects in RAM or on SSD disk.
  • Increase or reduce the time you cache an object with the Cache-Control header

Discover a new cloud experience

Deploy SSD cloud servers in seconds.