Routing assets to different origins

Some customers have assets stored on multiple origin servers and want to route various requests to specific, different servers based on criteria they supply (e.g., asset type, file directory, Host header). Fastly offers customers the ability to set conditions on their origins, which simply adds an if statement block to your VCL.

Basic setup: Create conditions for each origin

  1. Log in to the Fastly web interface.
  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
  3. Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
  4. Click the Origins link. The Origins page appears.
  5. Click the Attach a condition link to the right of the name of an origin server. The Create a new request condition window appears.

    a new condition to redirect hosts

  6. Fill out the Create a new request condition fields as follows:

    • In the Name field, enter a human-readable name for the condition.
    • In the Apply if field, enter the conditions that you want to apply to your origin server. For example, for hosts, you could enter req.http.host ~ "www.example.com". Or, for content-type / URL, you could enter req.url ~ ".(jpg|png|gif)($|\?)".
  7. Click the Save and apply to button. The new condition appears on the Origins page.

  8. Click the Activate button to deploy your configuration changes.

Backup setup: Create a header

What if you have a condition already assigned to your origin? Although you can group request conditions on the origin with an 'and' or 'or' clause, there can only ever be one condition rule attached to that origin. If you want to separate your request conditions instead of grouping them, you can use header rules to route assets to different origins instead.

  1. Log in to the Fastly web interface.
  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
  3. Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
  4. Click the Content link. The Content page appears.
  5. Click the Create header button. The Create a header page appears.

    the Create a header page

  6. Fill out the Create a header fields as follows:

    • In the Name field, enter Image Backend (or any meaningful, preferred name).
    • From the Type menu, select Request, and from the Action menu, select Set.
    • In the Destination field, enter backend.
    • In the Source field, enter Image_Backend. (This should match the name of your global origin server. You can see the exact name if you look at your VCL. Click on the VCL button at the top of the page.)
    • From the Ignore if set menu, select No.
    • In the Priority field, enter 10.
  7. Click the Create button. The new header appears on the Content page.

  8. On the Content page, click the Attach a condition link next to the header you just created. The Create a new request condition window appears.

    a new condition to redirect

  9. Fill out the Create a new request condition fields as follows:

    • In the Name field, enter Redirect Images (or any meaningful, preferred name).
    • In the Apply if field, enter req.url ~ "\.(jpg|png|gif)($|\?)".
  10. Click the Save and apply to button. The condition appears on the Content page.

  11. Click the Activate button to deploy your configuration changes.
TIP

Our about guide provides more information about working with conditions.

Use VCL Snippets to specify an origin

You can also use VCL Snippets to specify an origin. Once you've created your origin, you can conditionally route traffic to it.

  1. Log in to the Fastly web interface.
  2. From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
  3. Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
  4. Click the VCL Snippets link. The VCL Snippets page appears.

  5. Click Create Snippet. The Create a VCL snippet page appears.

    backend redirect

  6. In the Name field, enter an appropriate name (e.g., Send Images to Images Backend).

  7. From the Type controls, select within subroutine.

  8. From the Select subroutine menu, select recv (vcl_recv).

  9. In the VCL field, add the following condition:

    1if (req.url.ext ~ "(jpg|png|gif)") {
    2 set req.backend = Image_Backend;
    3}
  10. Click Create to create the snippet.

  11. Click the Activate button to deploy your configuration changes.
Was this guide helpful?

Do not use this form to send sensitive information. If you need assistance, contact support.