Skip to main content

Configure fetch compression

The Fetch compressed option tells CDN servers to pull already-compressed content from the origin instead of requesting uncompressed files. The origin must return GZip-encoded responses.

Fetch compressed

Turn the option on for the CDN resource.

  1. 1

    Open the CDN resource

    In the CDB Technical Web Portal, navigate to CDN. The CDN resources page opens.

  2. 2

    Open resource settings

    Click the CNAME of the resource to open its settings.

  3. 3

    Open Fetch compressed

    Open the OPTIONS tab and scroll to the Content section. Click Fetch compressed in the left sidebar.

  4. 4

    Enable Fetch compressed

    Turn on the Enable fetch compressed toggle.

    Fetch compressed section in CDN resource settings with the Enable fetch compressed toggle
  5. 5

    Save the resource

    Click Save changes.

Behavior when enabled

With Fetch compressed enabled, the CDN stores and serves the encoded response from the origin without decompressing it. Clients receive that compressed response even when the browser does not accept the encoding.

The CDN requests a GZip-encoded response from the origin. An origin that compresses only with Brotli returns uncompressed content, so configure it to return a Brotli file for that GZip request.

Fetch compressed cannot be used together with GZip compression, Brotli compression, or Slice.

Origin encoding

Leave Fetch compressed disabled when each viewer needs a response that matches the Accept-Encoding header. Set Vary: Accept-Encoding on the origin, because without that header the CDN can cache the first version and serve it to a client that needed the other version.

When fetch_compressed is on, the CDN requests a GZip-encoded response from the origin and serves it without decompressing it. The origin must return GZip-encoded responses.

An origin that compresses only with Brotli returns uncompressed content, so configure it to return a Brotli file when the CDN requests GZip encoding.

Clients receive that compressed response even when the browser does not accept the encoding.

An API token is required.

Export these variables. CDN_RESOURCE_ID is the id from list CDN resources.

export GCORE_API_KEY="{YOUR_API_KEY}"
export CDN_RESOURCE_ID="{YOUR_RESOURCE_ID}"

Fetch compressed

Enable Fetch compressed by setting both enabled and value to true.

The call returns HTTP 400 when gzipOn, brotli_compression, or slice is already enabled. Clear that option, then send fetch_compressed.

Leave fetch_compressed off when each viewer needs a response that matches Accept-Encoding. Set Vary: Accept-Encoding on the origin, because without that header the CDN can cache the first version and serve it to a client that needed the other version.

Send fetch_compressed with enabled and value set to true.

import os

from gcore import CDB

client = CDB()

resource = client.cdn.cdn_resources.update(
    int(os.environ["CDN_RESOURCE_ID"]),
    options={
        "fetch_compressed": {"enabled": True, "value": True},
    },
)
print(resource.options.fetch_compressed.enabled, resource.options.fetch_compressed.value)
package main

import (
    "context"
    "fmt"
    "os"
    "strconv"

    gcore "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cdn"
)

func main() {
    client := gcore.NewClient()
    ctx := context.Background()
    resourceID, err := strconv.ParseInt(os.Getenv("CDN_RESOURCE_ID"), 10, 64)
    if err != nil {
        panic(err)
    }

    resource, err := client.CDN.CDNResources.Update(ctx, resourceID, cdn.CDNResourceUpdateParams{
        Options: cdn.CDNResourceUpdateParamsOptions{
            FetchCompressed: cdn.CDNResourceUpdateParamsOptionsFetchCompressed{
                Enabled: true,
                Value:   true,
            },
        },
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(resource.Options.FetchCompressed.Enabled, resource.Options.FetchCompressed.Value)
}
curl -X PATCH "https://api.cdb-staging.cdn.orange.com/cdn/resources/$CDN_RESOURCE_ID" \
  -H "Authorization: APIKey $GCORE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "options": {
      "fetch_compressed": {
        "enabled": true,
        "value": true
      }
    }
  }'

The API returns HTTP 200:

{
  "id": 12345,
  "options": {
    "fetch_compressed": {
      "enabled": true,
      "value": true
    }
  }
}

Set fetch_compressed so the CDN requests a GZip-encoded response from the origin and serves it without decompressing it, using the Terraform provider v2. The origin must return GZip-encoded responses. Clients receive that compressed response even when the browser does not accept the encoding.

An origin that compresses only with Brotli returns uncompressed content, so configure it to return a Brotli file when the CDN requests GZip encoding. Enabling gzip_on while fetch_compressed is enabled returns HTTP 400. Clear gzip_on, brotli_compression, or slice before setting fetch_compressed. Leave fetch_compressed off when each viewer needs a response that matches Accept-Encoding. Set Vary: Accept-Encoding on the origin, because without that header the CDN can cache the first version and serve it to a client that needed the other version. Run terraform apply after editing options.

Fetch compressed

Set enabled and value to true. The CDN stores the encoded origin response and serves it without decompressing it.

resource "gcore_cdn_origin_group" "example" {
  name = "my-origin-group"

  sources = [
    {
      source  = "example.com"
      enabled = true
    }
  ]
}

resource "gcore_cdn_resource" "example" {
  cname           = "cdn.example.com"
  origin_group    = gcore_cdn_origin_group.example.id
  origin_protocol = "HTTPS"

  options = {
    fetch_compressed = {
      enabled = true
      value   = true
    }
  }
}