We've been making changes to how we organize and display our docs. Our work isn't done but we'd love your feedback.
Getting started
Basics
Domains & Origins
Performance

Configuration
Basics
Conditions
Dictionaries
Domains & Origins
Request settings
Cache settings
Headers
Responses
Performance
Custom VCL
Image optimization
Video

Security
Access Control Lists
Monitoring and testing
Securing communications
TLS
Web Application Firewall

Integrations
Logging endpoints
Non-Fastly services

Diagnostics
Streaming logs
Debugging techniques
Common errors

Account info
Account management
Billing
User access and control

Reference

    Routing assets to different origins

      Last updated May 23, 2019

    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 and click the Configure link.
    2. From the service menu, select the appropriate service.
    3. Click the Configuration button and then select Clone active. 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, type a human-readable name for the condition.
      • In the Apply if field, type the conditions that you want to apply to your origin server. For example, for hosts, you could type req.http.host ~ "www.example.com". Or, for content-type / URL, you could type 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 and click the Configure link.
    2. From the service menu, select the appropriate service.
    3. Click the Configuration button and then select Clone active. 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, type Image Backend (or any meaningful, preferred name).
      • From the Type menu, select Request, and from the Action menu, select Set.
      • In the Destination field, type backend.
      • In the Source field, type 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, type 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, type Redirect Images (or any meaningful, preferred name).
      • In the Apply if field, type 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.

    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 and click the Configure link.
    2. From the service menu, select the appropriate service.
    3. Click the Configuration button and then select Clone active. 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, type 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:

      1
      2
      3
      
      if (req.url.ext ~ "(jpg|png|gif)") {
        set req.backend = Image_Backend;
      }
      
    10. Click Create to create the snippet.
    11. Click the Activate button to deploy your configuration changes.
    Back to Top