How do I expect a specific status code (e.g. 200, 302) in an HTTP health check?
Short answer
In a Health Checks profile of the HTTP or HTTPS type, the expected codes are selected in the Expected Status Codes list; the default is 200. If the code of the response is one of those in the list, the check is considered successful. TR7 does not follow redirects: if the health address returns 302, 302 must be added to the list. The same list is written in WEB-CLI with --expectedStatusCodes.N; if a header or body condition is required together with the status code, JSONata Expression is used in the Content Check Mode field.
Problem: The Health Address Returns a Code Other Than 200
The default HTTP check considers only a 200 response successful. The health address of some applications redirects to the login page (302); some return 204 or 401. If the returned code is not in the list, the check fails with the "Unexpected status code" error; even while the server is running, it goes into the Error status after Required Failures consecutive failures and is removed from traffic.
Solution: 3 Methods
The three methods are alternatives to one another; Method 3 works together with the list in Method 1.
| Your situation | Suitable method |
|---|---|
| I want to set the code list from the interface | Method 1: Expected Status Codes |
| I create or update the profile from WEB-CLI | Method 2: WEB-CLI |
| A header or body condition is required together with the status code | Method 3: JSONata Expression |
Method 1: Expected Status Codes Recommended
- How it works If the status code of the response matches any code in the list, the check is successful; if it does not match, the "Unexpected status code" error occurs.
- Where it is openedLoad Balancer → Profiles → Health Checks → HTTP/HTTPS profile → Expected Status Codes
- RequirementCheck Type HTTP or HTTPS; a Backend Service to which the profile will be assigned
- vService type All (in Network vService, with ANY - L3 or while Port Range is on, the health status is not reflected in distribution)
Step-by-Step Configuration
The Configuration Menu is opened with the TR7 logo at the bottom left; the Load Balancer → Profiles → Health Checks path is followed.
For a new profile, Add is clicked; for an existing profile, the profile is selected and Edit is clicked (How to add?).
The profile name is written in the Name field; HTTP (default) or HTTPS is selected in the Check Type list. The type cannot be changed after saving.
The health address is written in the Path + Query field (e.g. /health); if it is left empty, the request is sent to the / address.
The expected codes are checked in the Expected Status Codes list (e.g. 200: OK and 302: Found (Moved Temporarily)). The default selection is 200: OK.
The Add (Edit when editing) button at the bottom of the form is clicked. If the profile is already assigned to Backend Services, the change is applied to all the Backend Services that use the profile.
The Load Balancer → Backend Services path is followed; the Backend Service is right-clicked and Edit is selected.
The profile is selected in the Health Check field. Enable HC Logs, which appears after the selection, is on by default.
The Edit button at the bottom of the form is clicked. The Backend Service first goes into the Preparing status; after Required Successes consecutive successful checks, it becomes OK. In a cluster, the change is transferred to the peer device automatically by default; if Manual Sync Mode is on, it is sent with Synchronize.
Verification
In the Backend Services list, the Backend Service is clicked; the Status Code row in the Details tab of the preview shows the code received in the last check (Details rows).
Status Code: 302 · Health Check: OK (green)In the Health Checks tab of the preview, the summary of each record contains the check type, the duration and the returned code.
OK · HTTP / 3ms / 302: Found (Moved Temporarily)Method 2: WEB-CLI
- How it works The expected codes are written in the
--expectedStatusCodes.Nfield with an index number; each code gets its own index number. - Where it is opened WEB-CLI →
lb hc add/lb hc update(lb hc · The HTTP and HTTPS Fields) - Requirement WEB-CLI access; the ID of the Backend Service to assign the profile
- vService type All
Step-by-Step Configuration
A new profile is created with the following command; checkType and name are required and are written in this order:
lb hc add checkType http name web-saglik --uri /health --expectedStatusCodes.0 200 --expectedStatusCodes.1 302First, the current list of the profile is viewed with lb hc show <profile-id>. The new code is written to the first empty index number of the list; a code written to an occupied index number replaces the code there. If the list contains only 200:
lb hc update id <profile-id> --expectedStatusCodes.1 302The profile ID is taken from the lb hc list output and the Backend Service ID from the lb backend list output, and the profile ID is written in the --hc field of the Backend Service (lb backend · The Health Check Fields):
lb backend update id <backend-id> --hc <profile-id>Verification
The profile record is displayed.
lb hc show <profile-id>200 and 302 in the expectedStatusCodes listMethod 3: JSONata Expression
- How it works When JSONata Expression is selected in the Content Check Mode field, the expression evaluates the status code (
status), the response headers (headers) and the body (body,bodyRaw) together; if the expression returns true (truthy), the content check is successful. The Expected Status Codes list continues to be applied separately. - Where it is opened HTTP/HTTPS profile → Content Check Mode → JSONata Expression → JSONata Query
- Requirement The expected codes also being in the Expected Status Codes list
- vService type All
Step-by-Step Configuration
On the Load Balancer → Profiles → Health Checks screen, the HTTP/HTTPS profile is selected and Edit is clicked.
The code expected in the expression is also checked in the Expected Status Codes list (e.g. 302: Found (Moved Temporarily)).
JSONata Expression is selected in the Content Check Mode field; for example, status = 302 and $contains(headers.location, "/login") is written in the JSONata Query field.
The Edit button is clicked; the change is applied to all the Backend Services that use the profile.
Verification
The indicator in the Health column of the Backend Services list is checked (Health status indicator). If the expression is not met, the error text appears in the Health Check card of the preview.
OK (green)Common Mistakes
- Assuming the redirect will be followed. TR7 does not follow 302; 302 must be among the expected codes.
- Relying on JSONata and leaving the list at 200. Expected Status Codes is also applied together with JSONata; if 3xx is not in the list, the check fails.
- Writing to an occupied index number in WEB-CLI. Writing
--expectedStatusCodes.0withlb hc updatereplaces the first code in the list; for adding, the first empty index number is used. - Assuming the server goes down immediately on a single failed check. The server becomes Error after Required Failures consecutive failures.
How it is done in the documentation
