Skip to main content

Set up a CDN resource to follow origin redirects

By default, when the origin returns a redirect, CDB caches and serves that redirect, while Redirection from origin makes the CDN follow selected codes, fetch the target, and cache it instead.

Redirection from origin

Turn the option on for the redirect codes the CDN should follow.

  1. 1

    Open the CDN resource

    In the CDB Technical Web Portal, navigate to CDN and select the CDN resource.

    CDN resources list
  2. 2

    Open redirection from origin

    In the sidebar, under the Cache section, select Redirection from origin.

    CDN resource settings with Cache section open
  3. 3

    Enable origin redirects

    Enable the Follow redirect from origin toggle.

    Follow redirect from origin toggle enabled with status codes field
  4. 4

    Select redirect codes

    In the Choose redirect status codes field, select one or more codes: 301, 302, 303, 307, or 308. Code 302 is selected by default.

    Choose redirect status codes dropdown
  5. 5

    Save the resource

    Click Save changes. Allow at least 15 minutes for the change to take effect.

    Changing this option can leave a previously cached redirect or target response in cache. Purge the cache after saving.

When follow_origin_redirect is enabled for a selected status code, the CDN follows it, caches the target response, and serves that content instead of the redirect itself.

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}"

Origin redirect option

codes accepts 301, 302, 303, 307, 308, and 404. The Customer Portal lists 301, 302, 303, 307, and 308.

enabled and codes are both required, because an empty codes list returns HTTP 400.

Changing this option can leave a previously cached redirect or target response in cache. Purge the cache after the update, and allow at least 15 minutes for the change to take effect.

Update the resource with follow_origin_redirect enabled for code 302.

import os
from gcore import CDB

client = CDB()

resource = client.cdn.cdn_resources.update(
    int(os.environ["CDN_RESOURCE_ID"]),
    options={
        "follow_origin_redirect": {"enabled": True, "codes": [302]},
    },
)
print(resource.options.follow_origin_redirect.enabled, resource.options.follow_origin_redirect.codes)
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{
            FollowOriginRedirect: cdn.CDNResourceUpdateParamsOptionsFollowOriginRedirect{
                Enabled: true,
                Codes:   []int64{302},
            },
        },
    })
    if err != nil {
        panic(err)
    }
    fmt.Println(resource.Options.FollowOriginRedirect.Enabled, resource.Options.FollowOriginRedirect.Codes)
}
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": {
      "follow_origin_redirect": {
        "enabled": true,
        "codes": [302]
      }
    }
  }'

The API returns HTTP 200:

{
  "id": 12345,
  "options": {
    "follow_origin_redirect": {
      "enabled": true,
      "codes": [302]
    }
  }
}

Set follow_origin_redirect so the CDN follows selected origin redirect codes, fetches the target, and caches that response instead of the redirect, using the Terraform provider v2. By default the CDN caches and serves the redirect itself.

codes accepts 301, 302, 303, 307, 308, and 404. The Customer Portal lists 301, 302, 303, 307, and 308, and 302 is selected by default. enabled and codes are both required. An empty codes list returns HTTP 400. Leaving follow_origin_redirect out of options leaves the stored option unchanged. Run terraform apply after editing options, and allow at least 15 minutes for the change to take effect.

Changing this option can leave a previously cached redirect or target response in cache. Purge the cache after the update.

Follow code 302

Set enabled to true and codes to 302. The CDN follows that redirect and caches the target response.

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 = {
    follow_origin_redirect = {
      enabled = true
      codes   = [302]
    }
  }
}