Routing assets to different origins
Last updated 2019-05-23
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
- Log in to the Fastly web interface.
- From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
- Click the Origins link. The Origins page appears.
Click the Attach a condition link to the right of the name of an origin server. The Create a new request condition window appears.
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 enterreq.url ~ ".(jpg|png|gif)($|\?)"
.
Click the Save and apply to button. The new condition appears on the Origins page.
- 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.
- Log in to the Fastly web interface.
- From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
- Click the Content link. The Content page appears.
Click the Create header button. The Create a header page appears.
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
.
- In the Name field, enter
Click the Create button. The new header appears on the Content page.
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.
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)($|\?)"
.
- In the Name field, enter
Click the Save and apply to button. The condition appears on the Content page.
- 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.
- Log in to the Fastly web interface.
- From the Home page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
Click the VCL Snippets link. The VCL Snippets page appears.
Click Create Snippet. The Create a VCL snippet page appears.
In the Name field, enter an appropriate name (e.g.,
Send Images to Images Backend
).From the Type controls, select within subroutine.
From the Select subroutine menu, select recv (
vcl_recv
).In the VCL field, add the following condition:
1if (req.url.ext ~ "(jpg|png|gif)") {2 set req.backend = Image_Backend;3}Click Create to create the snippet.
- Click the Activate button to deploy your configuration changes.
Do not use this form to send sensitive information. If you need assistance, contact support.