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
- 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.
- A personal token, for the API procedures.
- The Azion CLI installed and authorized, for the CLI procedures.
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.
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.
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.
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.