Route requests by country or continent
Send the requests of one continent or a list of countries to their own connector with a geolocation rule placed after the default rule.
You send the requests of one continent, or of a list of countries, to a regional connector with a geolocation rule, from Azion Console, the API, or the Azion CLI. To give a connector a backup origin that takes requests when its primary origin fails, refer to Add a backup origin to a connector.
A geolocation rule reads the location of the client’s IP address in the Request Phase and names a connector with Set Connector. Set Connector does not add up across rules: when several matching rules carry it, only the one from the last matching rule runs. A default rule that sends every path to one connector therefore comes first, and each geolocation rule comes after it and overrides the default for the requests it matches.
- Every request matches the first rule, which sets
origin-default. - A request from the continent or the countries of the second rule also matches it, which sets
origin-regional, and the later rule wins. - Any other request keeps
origin-default.
Prerequisites
Console
- An application served by a workload, with a first rule whose Set Connector behavior sends every request,
${uri}starts with/, to the default connector. To create them, refer to Applications quickstart. - A second connector, for the region. To create one, refer to Connectors quickstart.
The examples use origin-default for the default connector, origin-regional with the ID <connector-id> for the regional one, and the application <application-id>. Replace them with your values.
Create the geolocation rule
The criterion names the location to match. Rules Engine reads it from the client’s IP address, and the variables and operators below need no Product on the application:
| To match | Variable | Operator | Argument |
|---|---|---|---|
| One continent | ${geoip_continent_code}, the two-letter continent code | is equal, is_equal | A code such as NA, for North America |
| One country | ${geoip_country_code}, the two-letter country code | is equal, is_equal | A code such as RU, for Russia |
| A list of countries | ${geoip_country_code} | matches, matches | A regular expression of country codes, such as ^(BR|AR)$ |
A continent code is geography, not a jurisdiction. When a rule must follow the countries a law or a contract names, match the country list. The ${geoip_city_*} variables read a different geolocation base, geoip_city. For every geolocation variable, refer to Variables.
The procedures match the continent NA. For a country list, change the criterion to the one in the table.
Console
To create the rule in Azion Console:
Access Azion Console > Applications > your application, then go to the Rules Engine tab.
Enter a name that identifies the region. For example: geo - north america.
In the Criteria section, set the criterion to ${geoip_continent_code} is equal NA.
In the Behaviors section, select Set Connector, then select origin-regional.
The rule appears in the Request list of the Rules Engine tab.
Requests from North America go to origin-regional once the rule propagates, and every other request keeps origin-default. A new rule takes a few minutes to reach every data center.
In the Route users to regional origins use case, this section takes these values:
| Setting | Value |
|---|---|
| Rule name | geo - europe |
| Criterion | ${geoip_continent_code} is equal EU |
| Behavior | Set Connector with origin-eu, whose ID goes in attributes.value in the API |
| Position | after the default rule that sends every path to origin-us, so its order is higher than the default rule’s |
Requests from Europe go to origin-eu, and every other request to origin-us.
Keep the geolocation rule after the default rule
The platform creates a new rule at the end of its phase, which is the position a geolocation rule needs. A reordered list can put the default rule last, and it then sends every request to origin-default again, with no change to any rule. Check the order after every change to the list.
Console
To check the order in Azion Console:
Access Azion Console > Applications > your application, then go to the Rules Engine tab.
geo - north america sits after the default rule. If it does not, move it below the default rule.
The geolocation rule runs after the default rule, so its connector wins for the requests it matches.
To test the rule, send a request from a machine in the region it matches, because the rule reads the location of the client’s IP address. To see which rules ran on a request, turn on Debug Rules.
Confirm which region answered
The rule reads the location of the client’s IP address, so each check runs from a machine in the region it tests. The check reads a response header that names the region of the origin. The example sends requests from Europe to one connector and every other request to another, on www.example.com, and each origin adds an X-Origin-Region response header with the value us or eu. Replace each value with your own.
From a machine outside Europe, send several requests:
Every response carries x-origin-region: us. Under HTTP/2, header names arrive in lower case. From a machine in Europe, the same request carries x-origin-region: eu.
To compare the location Azion read with the origin that answered, access Azion Console > Real-Time Events, select the HTTP Requests data source, and enter host='www.example.com' in Filter by. Each record of your requests carries Geoloc Country Name and Upstream Addr, the address of the origin that answered. For the filter syntax, refer to Filter events.
These checks confirm the Route users to regional origins use case.