Changing origins based on user location

Fastly allows you to change origin servers based on the user's geographic location. This is useful when you need to serve different content to users who are in different locations. For example, you could change origin servers to serve a restricted version of your website to users in a different country.

Using the web interface

You can use the web interface to create the headers and the condition.

Creating the header for the default origin server

First, create a header for the default origin server to serve content to the majority of users. Follow these instructions to create the header:

  1. Log in to the Fastly web interface.
  2. From the All services 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 window appears.

    setting a default origin via the Create a header page

  6. Fill out the Create a header fields as follows:
    • In the Name field, enter the name of your header rule (for example, Set default origin).
    • From the Type menu, select Request, and from the Action menu, select Set.
    • In the Destination field, enter backend.
    • In the Source field, enter the name of the origin server you want to serve content to the majority of users (here it's F_global). Preview the VCL to find the name of the origin server.
    • From the Ignore if set menu, select No.
    • In the Priority field, enter 10.
  7. Click the Create button.

Creating the header for the restricted origin server

Now, create a header for the restricted origin server to serve content to the users residing in the countries specified in the condition. Follow these instructions to create the header:

  1. Click the Content link. The Content page appears.
  2. Click the Create header button. The Create a header window appears.

    setting a restricted origin via the Create a new header page

  3. Fill out the Create a header fields as follows:
    • In the Name field, enter the name of your header rule (for example, Set restricted origin).
    • From the Type menu, select Request, and from the Action menu, select Set.
    • In the Destination field, enter backend.
    • In the Source field, enter the name of the restricted origin server you want to serve content to the users residing in the countries specified in the condition (here it's F_restricted_content). Preview the VCL to find the name of the origin server.
    • From the Ignore if set menu, select No.
    • In the Priority field, enter 11.
  4. Click the Create button.

Creating a condition for the restricted origin header

Finally, create a condition for the restricted origin header. The condition checks the geolocation header. If the user's geolocation matches a location specified in the condition, Fastly uses the restricted origin server. Follow these instructions to create the condition:

  1. Click the Content link. The Content page appears.
  2. In the Headers section, click the Attach a condition link next to the Set restricted origin header. The Create a new request condition window appears.

    adding a restricted origin condition via the Create a new request condition window

  3. Fill out the Create a new request condition fields as follows:
    • In the Name field, enter a descriptive name for the new condition (for example, From Restricted Location).
    • In the Apply if field, enter a request condition. For example, to send all users in Asia and Europe to the restricted origin server, enter client.geo.continent_code == "AS" || client.geo.continent_code == "EU". See Geolocation-related VCL features for more information.
  4. Click the Save and apply to button.
  5. Click the Activate button to deploy your configuration changes.

Using custom VCL

If you'd prefer not to use the web interface, you can use custom VCL to configure your service to change origin servers based on the user's geographic location. Use the following VCL as a starting point:

1
2
3
4
5
6
7
  # default conditions
  set req.backend = F_global;

  # Use restricted content if the user is in Asia, France or Germany
  if (client.geo.continent_code == "AS" || client.geo.country_code == "FR" || client.geo.country_code == "DE") {
    set req.backend = F_restricted_content;
  }
Back to Top