How do I route to different servers based on the URL path (e.g. /api → API servers)?
Short answer
A Path condition is created (Criteria = Path, matching Start, Query = /api/) and bound to the API Backend Services with a Conditional Backend Services block on the HTTP vService; requests that do not match the condition go to Default Backend Services. Regex is used for more flexible matching, and Dynamic List for long path lists. If the user is to be sent to another address, the Location Redirection action is selected.
Problem: Different Paths Belong to Different Applications
Under the same domain name, different applications such as the website, the API and the management panel can be separated by path (/, /api, /admin). When these applications run on different servers, the requests need to be sent to the right server group based on the path; otherwise API requests end up at the web servers and get an error.
Solution: Three Methods
Method 1 and Method 2 set up the same configuration (Content Switching) from the interface or from the command line; Method 3, on the other hand, does not select a server, it redirects the client to another address.
| Your situation | Suitable method |
|---|---|
| I want to send requests to different Backend Services based on the path | Method 1: Path 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 another address (e.g. api.example.com) | Method 3: Location redirection |
Method 1: Path Condition + Conditional Backend Services Recommended
- How it works Requests that match the condition go to the Backend Services of the block; the blocks are evaluated from top to bottom, and the first matching block receives the request.
- Where it is openedLoad Balancer → Conditions · vService form → + Conditional backend services
- Requirement The target servers being defined as Backend Services
- vService type HTTP
Step-by-Step Configuration
If the API 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, the Backend Service Name and Address are entered for each server; a Health Check profile is selected so that it can be detected that the server is unreachable. If the servers are to be managed as a single target, a Backend Group is created under Load Balancer → Backend Groups. 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.
The Condition Name is entered (optional; the condition appears with this name in the selection lists). Criteria = Path is selected; the matching method Start (default Exact match) and Query = /api/ are entered. If case differences are to be ignored, Case Insensitive is checked (off by default).
If needed, the matching method is changed:
| Need | Selection |
|---|---|
/api and /api/... together, excluding /apiv2 | Matching Regex, Query ^/api(/|$) |
| Many paths in a single list | Criteria = Dynamic List; the path in the Key to search field, the paths in the List field |
The Add button is clicked.
The Load Balancer → Load Balancer vService List steps are followed and the HTTP vService on which the routing is to be done is opened.
The + Conditional backend services button in the Default Backend Services table is clicked; a new block opens on the form (Conditional Backend Services).
In the Select Conditions field of the block, the condition saved in step 4 is selected; in the Backend Service selector of the block, the API Backend Services (or the Backend Group) are selected. If there is more than one block, the more specific block is dragged to the top.
The Save button is clicked. The setting takes effect when it is saved; in a cluster setup, it is also transferred to the peer device with the default auto-sync.
Verification
On the Left Monitor Panel → Load Balancer vServices → vService → Logs & Tracking → HTTP Logs screen, the Backend Service that responded to the request is checked in the record of a request starting with /api/ (Viewing HTTP Logs). For records to be produced, the LOG field must be on in the vService (How Are HTTP Logs Enabled?).
/api/ requests → API Backend Service · other requests → Default Backend ServicesA /api/ request and a request for another path are sent; the applications the responses come from are compared.
curl -s http://203.0.113.10/api/healthThe response comes from the API applicationMethod 2: WEB-CLI
- How it works The Path condition and the conditional block are defined from the command line; the result is the same as in 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
Step-by-Step Configuration
lb condition add criteria path query /api/ --content_placement start --name api-yoluIf --content_placement is not given, the matching is exactMatch; to remove case sensitivity, --caseInsensitive true is added.
lb condition listThe ID of the condition (the value starting with lbConditions-) is taken from the output. The conditional block recognizes the condition by this ID, not by its name.
lb service show <vService>How many blocks there are in the dynamicBackendList list in the output is seen. For the new block, the first empty position number is used: 0 if the vService has no conditional blocks, 2 if it has two blocks. If an occupied position number is written, the block at that position is replaced.
The example below is for the case where the vService has no conditional block yet (position 0):
lb service update id <vService> --dynamicBackendList.0.cond "<condition-id>" --dynamicBackendList.0.beList.0 <backend-service>For a second Backend Service, --dynamicBackendList.0.beList.1 is added. In place of <backend-service>, the name of the Backend Service or of the Backend Group is written.
Verification
The vService is displayed again; the condition ID and the Backend Service of the new block must be in the list. The routing is checked with the verification cards of Method 1.
lb service show <vService>New block in dynamicBackendListMethod 3: Location Redirection
- How it works A request that matches the condition is redirected to another address (e.g.
https://api.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 condition that defines the traffic to be redirected, it is created with steps 2–4 of Method 1 (e.g. Path, Start, /api/).
The Load Balancer → Actions → Add steps are followed.
Action Type = Location redirection is selected; the target address is entered in the Redirect Location field, the redirect code is selected in the Redirect Code field (default 301, permanent; 302 for a temporary redirect) and the 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 that matches the condition is sent and the response headers are checked.
curl -sI http://203.0.113.10/api/healthHTTP/1.1 301 · Location: https://api.example.comCommon Mistakes
- Writing
/apiin Query. Start matching also catches paths such as/apiv2and/api-docs;/api/or the^/api(/|$)regex is used. - Overlooking upper and lower case. Matching is case-sensitive by default; for
/API/requests, Case insensitive is checked. - Thinking that Path includes the query string. Path does not include the part after
?; if the query string is also to be evaluated, Path + Query is used. - Thinking that TR7 removes the
/api/prefix by itself. The prefix is forwarded to the Backend Service as it is; if it needs to be removed, the Set Path action is used (Can I rewrite the URL path before it reaches the backend (rewrite)?). - Overlooking the order of the blocks. If a more general block is above, the specific block below it never matches.
- Binding the condition by its name in WEB-CLI.
--dynamicBackendList.N.condexpects the condition ID; if the name is written, the block matches no request.
How it is done in the documentation
