Can I show a custom error page when the backend returns 404 or 500?
Short answer
Yes, on an HTTP vService. In the Show Content for Backend Service Errors action, the error code (e.g. 404) is selected from the Error Codes list, and the Content Page to be shown and the Status Code are determined; when the Backend Service returns this code, the client sees the custom page instead of the page of the server. Since Status code comes as 200 by default, the actual code is selected; if 404 and 500 are to return with their own codes, a separate action is added for each code. To send the user to another address, Redirect for Backend Service Errors is used; for different content by path, Show Content with the Status Code condition at the On Response stage is used.
Problem: The Error Page of the Server Shows Technical Details
Application servers return their own default pages in case of an error; these pages look different in each application and sometimes contain version information or error details. Without touching the configuration of the Backend Service, the response is replaced with a corporate error page while it passes through TR7.
Solution: Three Methods
All three methods apply only to responses returned from the Backend Service. For the 502/503/504 that TR7 itself produces when the server cannot be reached at all, the Can I show a custom page for 503/502/504 errors generated by TR7 itself? page is consulted.
| Your situation | Suitable method |
|---|---|
| The same custom page for all paths on the selected error codes | Method 1: Show Content for Backend Service Errors |
| Sending the user to another address on an error code | Method 2: Redirect for Backend Service Errors |
A different page by path or by another condition (e.g. JSON for /api/) | Method 3: Show content (On response) + Status code condition |
Method 1: Show Content for Backend Service Errors Recommended
- How it works If the code of the response returned from the Backend Service is one of the selected codes, the response is replaced with the selected page and Status code; the action takes no condition.
- Where it is openedLoad Balancer → Actions → Show Content for Backend Service Errors
- Requirement The error page is in Content Pages
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Profiles → Content Pages → Add is clicked (how to add).
Content Page Name, Content Type (default text/html) and Content are entered. Since this action does not add a character set to the response, <meta charset="utf-8"> is written in the head section of the page for Turkish characters.
Add is clicked.
Load Balancer → Actions → Add is clicked (how to add).
Action Name is entered, and Action Type = Show Content for Backend Service Errors is selected.
Error Codes = 404, Content Page = the page from step 3, Status Code = 404 (default 200). More than one code can be selected in the Error codes list; however, all of them return with the single Status code value of this action.
Add is clicked.
Steps 4–7 are repeated with Error Codes = 500, Status Code = 500.
On the Load Balancer → Load Balancer vService List screen, the HTTP vService is selected and Edit is clicked.
Save is clicked. The change is applied without dropping open connections; in a cluster, it is also sent to the peer device with the default auto-sync (with Synchronize if Manual Sync Mode is on). No change is required on the Backend Service side.
Verification
curl -i https://www.example.com/olmayan-sayfa
HTTP/1.1 404 + custom pageOn the HTTP Logs screen, the request appears with the 404 code.
Status code 404Method 2: Redirect for Backend Service Errors
- How it works For requests on which one of the selected codes is returned, the client is redirected to another address with 302; the error page of the Backend Service does not go to the client.
- Where it is openedLoad Balancer → Actions → Redirect for Backend Service Errors
- Requirement The page to redirect to (e.g. a search or support page) must exist in a way that does not create a redirection loop
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Actions → Add is clicked.
Action Type = Redirect for Backend Service Errors is selected.
Error Codes = 404; Redirect Location = /arama. The field supports Smart Variable with fx; for example, by adding Path/page of the request [URI] after /arama?kaynak=, the path that was not found is carried to the search page.
Add is clicked.
The vService is opened with Edit.
The action is selected in the Details → Actions field.
Save is clicked.
Verification
curl -sI https://www.example.com/olmayan-sayfa
302 + Location: /arama…Method 3: Show Content (On Response) + Status Code Condition
- How it works Show content that works at the response stage, together with the Status code condition and other conditions, returns content only on the desired requests; for example, JSON for requests under
/api/that return 500, and HTML for the others. - Where it is openedLoad Balancer → Conditions → Status Code · Actions → Show Content
- Requirement The Status code condition can be selected only at the On response stage
- vService type HTTP
Step-by-Step Configuration
With steps 1–3 of Method 1, a page with Content Type = application/json and Content = {"error":"internal_error"} is added (What is the Content Pages profile; how do I prepare my own HTML page?).
Load Balancer → Conditions → Add is clicked (how to add).
Criteria = Status Code, Operator = Equals to (default) and the value 500 are entered.
Add is clicked.
The second condition form is opened with Load Balancer → Conditions → Add.
Criteria = Path, the matching Start and the value /api/ are entered.
Add is clicked.
Load Balancer → Actions → Add is clicked.
Action Type = Show Content is selected.
Execution Level = On response; Content page = the JSON page; Status code = 500; in the Conditions field, the two conditions are selected with and (custom error pages scenario).
Add is clicked.
The vService is opened with Edit.
The action is selected in the Details → Actions field.
Save is clicked.
Verification
An API path that returns 500 is requested with curl -i https://www.example.com/api/rapor.
HTTP/1.1 500 + {"error":"internal_error"}Common Mistakes
The Status code default is 200
If Status code is not changed, the 404 and 500 responses go to the client with the 200 code; search engines take the error page for content, and monitoring tools do not see the error. The actual error code is selected in each action.
- Expecting the errors that TR7 produces from this action. The 502/503/504 returned when the Backend Service cannot be reached are within the scope not of these actions but of the Show Content for Traffic Manager Errors and Redirect for Traffic Manager Errors actions.
- Combining several codes in a single action and expecting the codes to be preserved. All the codes in a single action return with the same Status code; if the codes are to be preserved, a separate action is added for each code.
- Saving without selecting any code. While Error codes is empty, the action does not change any response.
- Forgetting the Turkish characters. No character set is added to the Show Content for Backend Service Errors response;
<meta charset="utf-8">is written in the page.
How it is done in the documentation
