Skip to main content
CDN resource rules allow you to apply custom configurations to specific URL patterns or file types. Rules override the main CDN resource settings for matching requests.

Create Rule

Create a new rule for a CDN resource.
import (
    "github.com/G-Core/gcore-go"
    "github.com/G-Core/gcore-go/cdn"
)

params := cdn.CDNResourceRuleNewParams{
    Name:     "Cache images for 7 days",
    Rule:     "/images/*.(jpg|png|webp)",
    RuleType: 0, // Regular expression
    Active:   gcore.Bool(true),
    Weight:   gcore.Int(1),
    Options: cdn.CDNResourceRuleNewParamsOptions{
        EdgeCacheSettings: cdn.CDNResourceRuleNewParamsOptionsEdgeCacheSettings{
            Enabled: true,
            Value:   gcore.String("604800s"), // 7 days
        },
    },
}

rule, err := client.CDN.CDNResources.Rules.New(
    context.Background(),
    resourceID,
    params,
)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Created rule ID: %d\n", rule.ID)
name
string
required
Rule name for identification
rule
string
required
URL path pattern for matching requests. A leading forward slash is automatically added
rule_type
int64
required
Rule pattern type:
  • 0 - Regular expression (must start with ^/ or /)
  • 1 - Legacy regular expression (automatically prefixed with /)
active
boolean
Whether the rule is active. true applies rule settings, false disables the rule
weight
int64
Rule execution order from lowest (1) to highest. Higher weight rules take precedence when multiple rules match
origin_group
int64
Origin group ID to use for this rule. If not specified, inherits from CDN resource
override_origin_protocol
string
Override origin protocol: HTTP, HTTPS, or MATCH. null inherits from CDN resource
options
object
Rule-specific configuration options. See Rule Options section

Get Rule

Retrieve details of a specific rule.
rule, err := client.CDN.CDNResources.Rules.Get(
    context.Background(),
    ruleID,
    cdn.CDNResourceRuleGetParams{
        ResourceID: resourceID,
    },
)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Rule: %s (ID: %d)\n", rule.Name, rule.ID)
fmt.Printf("Pattern: %s, Active: %v\n", rule.Rule, rule.Active)
id
int64
Rule ID
name
string
Rule name
rule
string
URL path pattern
rule_type
int64
Rule pattern type (0 or 1)
active
boolean
Whether the rule is active and applied
weight
int64
Rule execution order priority
options
object
Rule configuration options

List Rules

Get all rules for a CDN resource.
rules, err := client.CDN.CDNResources.Rules.List(
    context.Background(),
    resourceID,
)
if err != nil {
    log.Fatal(err)
}

for _, rule := range rules.Results {
    fmt.Printf("Rule: %s (Weight: %d)\n", rule.Name, rule.Weight)
}

Update Rule

Modify an existing rule.
params := cdn.CDNResourceRuleUpdateParams{
    ResourceID: resourceID,
    Active:     param.NewOpt(true),
    Weight:     param.NewOpt[int64](5),
    Options: cdn.CDNResourceRuleUpdateParamsOptions{
        EdgeCacheSettings: cdn.CDNResourceRuleUpdateParamsOptionsEdgeCacheSettings{
            Enabled: true,
            Value:   gcore.String("86400s"),
        },
    },
}

rule, err := client.CDN.CDNResources.Rules.Update(
    context.Background(),
    ruleID,
    params,
)
if err != nil {
    log.Fatal(err)
}

Delete Rule

Delete a rule permanently. The rule must be deactivated first.
err := client.CDN.CDNResources.Rules.Delete(
    context.Background(),
    ruleID,
    cdn.CDNResourceRuleDeleteParams{
        ResourceID: resourceID,
    },
)
if err != nil {
    log.Fatal(err)
}
Set the active attribute to false before deletion. This action is irreversible.

Rule Options

Rules support the same configuration options as CDN resources. Options specified in rules override the main resource settings for matching requests.

Cache Configuration

Options: cdn.CDNResourceRuleNewParamsOptions{
    EdgeCacheSettings: cdn.CDNResourceRuleNewParamsOptionsEdgeCacheSettings{
        Enabled: true,
        CustomValues: map[string]string{
            "200": "86400s",  // Cache 200 responses for 24 hours
            "404": "3600s",   // Cache 404 responses for 1 hour
        },
    },
    BrowserCacheSettings: cdn.CDNResourceRuleNewParamsOptionsBrowserCacheSettings{
        Enabled: true,
        Value:   "7200s", // 2 hours
    },
}

Compression Settings

Options: cdn.CDNResourceRuleNewParamsOptions{
    GzipOn: cdn.CDNResourceRuleNewParamsOptionsGzipOn{
        Enabled: true,
        Value:   true,
    },
    BrotliCompression: cdn.CDNResourceRuleNewParamsOptionsBrotliCompression{
        Enabled: true,
        Value:   []string{"text/html", "text/css", "application/javascript"},
    },
}
If fetch_compressed is enabled in the resource settings, you must set it to false in the rule to use gzipOn or brotli_compression.

Query String Handling

Options: cdn.CDNResourceRuleNewParamsOptions{
    QueryParamsWhitelist: cdn.CDNResourceRuleNewParamsOptionsQueryParamsWhitelist{
        Enabled: true,
        Value:   []string{"id", "version", "locale"},
    },
}

Access Control

Options: cdn.CDNResourceRuleNewParamsOptions{
    IPAddressACL: cdn.CDNResourceRuleNewParamsOptionsIPAddressACL{
        Enabled:        true,
        PolicyType:     "allow",
        ExceptedValues: []string{"10.0.0.0/8"},
    },
    ReferrerACL: cdn.CDNResourceRuleNewParamsOptionsReferrerACL{
        Enabled:        true,
        PolicyType:     "deny",
        ExceptedValues: []string{"badsite.com", "*.spam.com"},
    },
}

HTTP Headers

Options: cdn.CDNResourceRuleNewParamsOptions{
    StaticResponseHeaders: cdn.CDNResourceRuleNewParamsOptionsStaticResponseHeaders{
        Enabled: true,
        Value: map[string]string{
            "X-Custom-Header": "value",
            "Cache-Control":   "public, max-age=3600",
        },
    },
    HostHeader: cdn.CDNResourceRuleNewParamsOptionsHostHeader{
        Enabled: true,
        Value:   "origin.example.com",
    },
}

CORS Configuration

Options: cdn.CDNResourceRuleNewParamsOptions{
    Cors: cdn.CDNResourceRuleNewParamsOptionsCors{
        Enabled: true,
        Value:   []string{"https://app.example.com", "https://admin.example.com"},
        Always:  gcore.Bool(false), // Only add for 2xx/3xx responses
    },
}

Image Optimization

Options: cdn.CDNResourceRuleNewParamsOptions{
    ImageStack: cdn.CDNResourceRuleNewParamsOptionsImageStack{
        Enabled:     true,
        Quality:     gcore.Int(85),
        WebpEnabled: gcore.Bool(true),
        AvifEnabled: gcore.Bool(true),
        PngLossless: gcore.Bool(false),
    },
}

Force Return (Redirects)

Options: cdn.CDNResourceRuleNewParamsOptions{
    ForceReturn: cdn.CDNResourceRuleNewParamsOptionsForceReturn{
        Enabled: true,
        Code:    301,
        Body:    "https://newsite.example.com",
    },
}

Rule Examples

Long-term Caching for Static Assets

params := cdn.CDNResourceRuleNewParams{
    Name:     "Cache static assets for 30 days",
    Rule:     "\\.(css|js|jpg|jpeg|png|gif|svg|woff|woff2|ttf|eot)$",
    RuleType: 0,
    Active:   gcore.Bool(true),
    Weight:   gcore.Int(10),
    Options: cdn.CDNResourceRuleNewParamsOptions{
        EdgeCacheSettings: cdn.CDNResourceRuleNewParamsOptionsEdgeCacheSettings{
            Enabled: true,
            Value:   gcore.String("2592000s"), // 30 days
        },
        BrowserCacheSettings: cdn.CDNResourceRuleNewParamsOptionsBrowserCacheSettings{
            Enabled: true,
            Value:   "2592000s",
        },
    },
}

API Endpoint with No Caching

params := cdn.CDNResourceRuleNewParams{
    Name:     "No cache for API",
    Rule:     "/api/.*",
    RuleType: 0,
    Active:   gcore.Bool(true),
    Weight:   gcore.Int(5),
    Options: cdn.CDNResourceRuleNewParamsOptions{
        EdgeCacheSettings: cdn.CDNResourceRuleNewParamsOptionsEdgeCacheSettings{
            Enabled: true,
            Value:   gcore.String("0s"), // Disable caching
        },
        AllowedHTTPMethods: cdn.CDNResourceRuleNewParamsOptionsAllowedHTTPMethods{
            Enabled: true,
            Value:   []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
        },
    },
}

Geo-blocking for Specific Content

params := cdn.CDNResourceRuleNewParams{
    Name:     "Block EU users from premium content",
    Rule:     "/premium/.*",
    RuleType: 0,
    Active:   gcore.Bool(true),
    Weight:   gcore.Int(15),
    Options: cdn.CDNResourceRuleNewParamsOptions{
        CountryACL: cdn.CDNResourceRuleNewParamsOptionsCountryACL{
            Enabled:        true,
            PolicyType:     "deny",
            ExceptedValues: []string{"US", "CA"},
        },
        ForceReturn: cdn.CDNResourceRuleNewParamsOptionsForceReturn{
            Enabled: true,
            Code:    403,
            Body:    "Access denied for your region",
        },
    },
}

Optimize Images

params := cdn.CDNResourceRuleNewParams{
    Name:     "Optimize and convert images",
    Rule:     "\\.(jpg|jpeg|png)$",
    RuleType: 0,
    Active:   gcore.Bool(true),
    Weight:   gcore.Int(20),
    Options: cdn.CDNResourceRuleNewParamsOptions{
        ImageStack: cdn.CDNResourceRuleNewParamsOptionsImageStack{
            Enabled:     true,
            Quality:     gcore.Int(80),
            WebpEnabled: gcore.Bool(true),
            AvifEnabled: gcore.Bool(true),
        },
        EdgeCacheSettings: cdn.CDNResourceRuleNewParamsOptionsEdgeCacheSettings{
            Enabled: true,
            Value:   gcore.String("604800s"), // 7 days
        },
    },
}

Rule Priority

When multiple rules match a request, the rule with the highest weight is applied. Plan your rule weights carefully:
  1. Low priority (1-10): General rules for common patterns
  2. Medium priority (11-50): Specific content type rules
  3. High priority (51+): Override rules for special cases
// Low priority - general static assets
Weight: gcore.Int(5)

// Medium priority - specific image handling
Weight: gcore.Int(20)

// High priority - override for specific files
Weight: gcore.Int(100)

CDN Resources

Manage parent CDN resource configurations

SSL Certificates

Manage HTTPS certificates

Build docs developers (and LLMs) love