Configure service routes

Media CDN provides advanced HTTP routing capabilities that allow you to map traffic to specific edge configurations and origins at a fine-grained level.

Configure a route rule

Configure a route rule for a Media CDN service.

Console

  1. In the Google Cloud console, go to the Media CDN page.

    Go to Media CDN

  2. To open the Details page of the service for which you want to configure a route rule, click the service name.

  3. To switch to the edit mode, click the Edit button.

  4. To navigate to the Routing section, click Next.

  5. Specify at least one host rule. Click Add host rule. Then, do the following:

    1. For Hosts, specify at least one host for matching.

    2. For Description, provide a brief description for the host rule.

    Alternatively, to edit a host rule, click the arrow to expand it.

  6. Specify at least one route rule. Click Add route rule.

    Alternatively, to edit a route rule, click Edit on the respective row.

  7. In the Edit route rule pane, for Priority, set a value for route priority.

  8. For Description, provide a brief description that can help identify the rule in a list of rules.

  9. In the Match section, specify at least one match condition. Click Add a match condition. Then, do the following:

    1. For Match type, select any path match option.
    2. For Path match, specify the names, paths, or templates. Consider using wildcard pattern matching.

      If required, also select Enable case sensitivity for path value.

    3. Optional: Select Headers match and Query parameters match. Then, click the relevant buttons to add headers and query parameters. For each, specify the name, match type, and value.

      For more information, see Match on headers and query parameters.

    4. To save the match condition, click Done.

  10. For Primary action, select one of the following options:

    • Fetch from an origin: To direct requests to a specific origin, select this option, and then select an origin.

    • URL redirect: To redirect requests, select this option. Then, specify the type of redirect, the path, and the status code.

      Optionally, select the options to redirect all responses to HTTPS, or to strip the query.

  11. Click Advanced configurations.

    1. In the Header action section, click Add an item.

      Select a type of action and then specify a header as a name and value pair. Then, click Done.

    2. In the Route action section, click Add an item.

      Specify a type of action and its related options. Then, click Done.

  12. For HTTP method filtering, select Customize HTTP method filtering.

    Then, select the HTTP methods that you want proxied to your origin.

  13. To save the route rule, click Save.

  14. To save your changes to the service, click Update service.

gcloud and YAML

  1. Export your Media CDN configuration into a YAML file. Use the gcloud edge-cache services export command.

    gcloud edge-cache services export SERVICE_NAME \
        --destination=FILENAME.yaml
    

    Replace the following:

    • SERVICE_NAME: the name of your service
    • FILENAME : the name of your YAML file
  2. Update the YAML file with the required configuration as described in the sections on this page.

  3. To update the service, import your Media CDN configuration from the YAML file. Use the gcloud edge-cache services import command.

    gcloud edge-cache services import SERVICE_NAME \
        --source=FILENAME.yaml
    

Match requests

A Media CDN configuration contains a set of routes defined in the Routing section for an EdgeCacheService resource. These routes match requests based on (at least) a host. For more details about how traffic is directed to an origin, see HostRule and PathMatcher. Each route is able to define its own CDN configuration, rewrites, redirects, CORS policies, custom HTTP headers, and origin mapping. Routes can share origins.

For example, you can route requests for manifests to a specific origin and define a short-lived cache TTL and a negative caching policy. Requests for segments can be split to another origin by using headers and query parameters to break out specific manifest types or users.

The following example shows how to route requests that match a specific header, query parameter, and path prefix for the host media.example.com:

name: prod-service
routing:
  hostRules:
  - hosts:
    - media.example.com
    pathMatcher: example_routes
  pathMatchers:
  - name: example_routes
    routeRules:
    - priority: 10
      origin: staging-live-origin
      matchRules:
      - prefixMatch: /vod/
        headerMatches:
        - headerName: "x-staging-client"
          presentMatch: true
        queryParameterMatches:
        - name: "live"
          exactMatch: "yes"
      routeAction:
        cdnPolicy:
          defaultTtl: 5s

Path matching

Media CDN supports full (exact), prefix, and wildcard path matching. Path matching can be combined with host, header, and query parameter-based matching to construct fine-grained request routing rules.

Following are three ways to match on a URL path.

Field Description Example
matchRules[].fullPathMatch The fullPathMatch condition matches the full URL path, which does not include the query string. You must specify trailing slashes, if relevant.

A route with a match rule of fullPathMatch: "/stream/" matches /stream/ but not /stream or /stream/us/hls/1234.ts.

A fullPathMatch is an explicit (exact) match.

matchRules[].prefixMatch The prefixMatch condition matches the URL path prefix; URLs that start with the same string match.

A route with a match rule of prefixMatch: "/videos/" matches both /videos/hls/58481314/manifest.m3u8 and /videos/dash because they both contain the /videos/ prefix.

matchRules[].pathTemplateMatch The