Can I route clients to a different backend based on their country / city?
Short answer
Yes. In the Source IP condition, Source IP Type = Country is selected and the country is specified, and the condition is bound to a Conditional Backend Services block; the country is detected in IPv4 and IPv6 addresses. City-based routing is done with the City type and works only for the 81 provinces of Turkey and IPv4 addresses; the ASN type is used to distinguish by service provider. Each condition selects a single country, a single province or a single ASN; for more than one value that will go to the same target, separate conditions are combined with OR in the block. Location Redirection is used to send the user to a regional domain name.
Problem: Content or Infrastructure Is Divided by Region
Because of regional campaigns, local regulations or geographically distributed servers, users may need to be sent to different servers based on the country or city they are in. The location of the client is understood only from the IP address, through a geographic IP database.
Solution: Three Methods
Method 1 and Method 2 set up the same configuration (Source IP condition + conditional block) from the interface or from the command line; Method 3 does not select a server, it redirects the client to another address.
| Your situation | Suitable method |
|---|---|
| I want to send to different Backend Services based on the country, the province within Turkey or the service provider (ASN) | Method 1: Source IP condition + Conditional Backend Services |
| I want to make the same configuration from the command line | Method 2: WEB-CLI |
I want to send the user to a regional domain name (e.g. de.example.com) | Method 3: Location redirection |
| There is a CDN or a proxy in front of TR7 | First, a Trusted Proxies profile is assigned to the vService → There is a CDN such as Cloudflare / Akamai in front of TR7; how do I get the Real client IP (trusted proxy)? |
Method 1: Source IP Condition + Conditional Backend Services Recommended
- How it works The client IP is looked up in the geographic IP database; a request that fits the condition goes to the Backend Services of the block, the others go to the next blocks or to Default Backend Services.
- Where it is openedLoad Balancer → Conditions · vService form → + Conditional backend services
- Requirement The target servers being defined as Backend Services
- vService type HTTP · L7 TCP
Step-by-Step Configuration
If the regional servers are not yet defined as Backend Services in TR7, they are added first; if they are defined, this step is skipped. With Load Balancer → Backend Services → Add, Backend Service Name and Address are entered for each server and a Health Check profile is selected. Details: How is a Backend Service added; which information is required?
The Load Balancer → Conditions → Add steps are followed; the form is described in the Conditions · How to Add? section.
Condition Name is entered, Criteria = Source IP is selected, and the type of distinction is selected in the Source IP Type field:
| Source IP Type | Selection | Scope |
|---|---|---|
| Country | A country from the list; the default value is Turkey | IPv4 and IPv6 |
| City | A province from the list; the default value is Ankara; only the 81 provinces of Turkey | IPv4 only |
| ASN | An ASN number from the searchable list, without the AS prefix (e.g. 16509) | IPv4 and IPv6 |
The Add button is clicked. Steps 2–4 are repeated for each country, province or ASN that will go to the same target.
The Load Balancer → Load Balancer vService List steps are followed and the vService is opened.
The + Conditional backend services button in the Default Backend Services table is clicked (Conditional Backend Services).
The condition is selected in the Select Conditions field of the block; the conditions of the countries that will go to the same target are selected together by setting the AND / OR switch in the group to OR. The regional Backend Services are selected in the Backend Service selector. Separate blocks are added for different targets.
The Save button is clicked.
Verification
On the Left Monitor Panel → Load Balancer vServices → vService → Logs & Tracking → HTTP Logs screen, the Location (the geographic location of the client) value of the request is compared with the Backend Service that responded to the request (Viewing HTTP Logs). For the record to be created, the LOG field must be on in the vService. An L7 TCP vService has no HTTP Logs; the Backend Service the connection goes to is followed in the Backend Name column of the vService Sessions screen, together with the client address (Source IP) (vService Sessions Listing Screen).
Request from the selected country → regional Backend ServiceMethod 2: WEB-CLI
- How it works The Source IP condition and the conditional block are defined from the command line; the result is the same as Method 1.
- Where it is opened TR7 WEB-CLI →
lb condition·lb service - Requirement WEB-CLI access; the target Backend Services being defined
- vService type HTTP · L7 TCP
Step-by-Step Configuration
The two-letter country code is written for a country, the license plate code for a province, and the number for an ASN:
lb condition add criteria src_ip srcIpType country country DE --name almanya
lb condition add criteria src_ip srcIpType city city 34 --name istanbul
lb condition add criteria src_ip srcIpType asn asn 16509 --name aws-asnlb condition listThe ID of the condition (the value starting with lbConditions-) is taken from the output; the block recognizes the condition by this ID, not by its name.
lb service show <vService>The number of blocks in the dynamicBackendList list is seen; for the new block, the first empty index is used (0 if there are no blocks). If an occupied index is written, the block at that index is replaced.
For the case where the vService has no conditional block yet (index 0); the conditions of two countries are combined with or:
lb service update id <vService> --dynamicBackendList.0.cond "<condition-id-1> or <condition-id-2>" --dynamicBackendList.0.beList.0 <backend-service>For a single condition, only the ID of that condition is written inside the quotes. The name of the Backend Service or the Backend Group is written in place of <backend-service>.
Verification
The vService is displayed again; the routing is checked with the verification card of Method 1.
lb service show <vService>New block in dynamicBackendListMethod 3: Location Redirection
- How it works A request that fits the condition is redirected to a regional address (e.g.
https://de.example.com) with a 3xx; this is not a server selection, the client sends a new request to the new address. Details: How do I redirect a specific URL to another URL with a 301/302? - Where it is openedLoad Balancer → Actions · vService form → Details → Actions
- Requirement A condition (if Location redirection is saved without a condition, it never runs)
- vService type HTTP
Step-by-Step Configuration
If there is no Source IP condition that defines the country to be redirected, it is created with steps 2–4 of Method 1.
The Load Balancer → Actions → Add steps are followed.
Action Type = Location redirection is selected; the regional address is entered in the Redirect Location field and the redirect code (e.g. 302 for a temporary redirect) in the Redirect Code field, and the Source IP condition is selected in the Conditions field.
The Add button is clicked.
The Load Balancer → Load Balancer vService List steps are followed, the vService is opened and the Details button at the top of the form is clicked.
The created action is selected in the Actions field.
The Save button is clicked.
Verification
A request is sent from a client in the selected country and the response headers are checked.
curl -sI https://www.example.com/HTTP/1.1 302 · Location: https://de.example.comCommon Mistakes
- Not assigning a Trusted Proxies profile while TR7 is behind a CDN or a proxy. In this case, the condition is evaluated according to the IP address of the proxy. When a Trusted Proxy Profile is assigned to the HTTP vService, all components, including conditions, use the resolved real client IP (The Components That Use the Resolved Address).
- Using City for cities abroad. City recognizes only the provinces of Turkey and IPv4 addresses; Country is used for other countries.
- Not keeping the geographic data up to date. Country, province and ASN data are updated with the IP DB component on the Settings → General Settings → System Update screen (The Component Rows).
- Assuming that the location is always correct. For VPN users and some mobile operator users, the location may appear different from where the user is; it is not used alone for an access restriction.
- Binding the condition by its name in WEB-CLI.
--dynamicBackendList.N.condexpects the condition ID.
How it is done in the documentation
