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 power to set conditions on their origins, which simply adds an if statement block to your VCL.
Basic setup: Create conditions for each origin
To add a condition on your origin, navigate to Hosts → Backends within the Fastly application. Click the gear icon next to a specific origin, and select Conditions from the menu that appears to create the new condition you wish to apply to that origin. Here are some example criteria and the conditions that would be placed in the Apply If field of the New Condition you create:
|content-type / url||
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.
Within the Fastly application, navigate to Content → Headers. Then select the New button to create the first header that will set the default origin.
Set the first header's controls as follows:
Image Backend(or any meaningful, preferred name)
- Type/Action: Request/Set
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.)
- Ignore If Set:
Once you've set the controls, click Create to add the first new header.
Next click the gear icon to add a condition. Select New to add a new condition.
Enter the condition settings as follows:
Redirect Images(or any meaningful, preferred name)
- Apply if:
req.url ~ "\.(jpg|png|gif)($|\?)"
Once you have finished, click Create to assign the condition to your header. Then click the Activate button, near the top right corner of the window, to deploy the new version of the service you just edited.