Skip to main content
shafiq23
Staff & Editor
Staff & Editor
Feb 16, 2024 5:51 AM UTC

Technical Tip: SLB script - add response HTTP header with real server identifier

  • Oct 1, 2026 2:25 PM UTC
  • 0 replies
  • 633 views
  • Translate All

Description

This article describes how to use server load-balancing scripting to achieve the requirement of adding an HTTP Header in the response direction. The objective is to enable the client/developer to observe the server identifier in the response HTTP header, indicating which real server responded to the request.

Scope

FortiADC, FortiADC VM.

Solution

Traffic flow:
Client -> FortiADC Virtual Server -> Real Server Pool -> Server1 / Server2.

 

  • Virtual Server FQDN: dvwa.ftntlab.local.

  • Server1: 10.100.1.209.

  • Server: 10.100.1.43.

 
Requirement:
When the request matches the specified domain name, add the response HTTP header 'X-Backend-Server' with the real server identifier.

 

  1. Create a Server Load Balance script: Server Load Balance -> Scripting -> Create New.


e376b71b.png

  1. Define a name.

  2. Input the Lua script statement.


local shared_host
when HTTP_REQUEST {
host = HTTP:header_get_value("Host");
if host == "<domain-name>" then
shared_host = host
else
end
}
when HTTP_RESPONSE {
if shared_host then
sip = HTTP:server_addr()
debug("backend server IP is %s ", sip);
local backend_servers = {
["<serverIP-1"] = 1,
["<serverIP-2"] = 2,
}
if backend_servers[sip] then
local server_identifier = backend_servers[sip]
HTTP:header_insert("X-Backend-Server", tostring(server_identifier))
log("Server IP is: " .. sip .. ", Server identifier is: " .. server_identifier)
end
end
}


a51c04eb.png


Details and comments on the Lua script are used in this article.
 

Lua script.PNG

 

Note that there is a static record for variable backend_servers that needs to be maintained if the server IP address is changed.

 

  1. Assign Server Load Balance script to the Virtual Server.


Add script.PNG


Note that the round-robin load-balancing method was used for this demonstration.

Demonstration:

  • Send request using HTTPie.

  • Verify 'X-Backend-Server' in the HTTP response header.

 
First request:

Server1.PNG


Second request:

Server2.PNG

 
In the FortiADC script log, there will be a log generated if the script is triggered: Log & Report -> Script Log.

86c2e54c.png


script log.PNG

 
Note: Refer to the following article for more information: How to Generate script log.

Related documentation on Server Load Balance script usage:
Using HTTP scripting

    Risk and Resilience in the Age of AI. An OT Thought Leadership Summit for Houston's Critical Infrastructure Leaders. Date: Thursday, December 3, 2026. Time: 8:30 am - 1:30 pm (CT). Location: The Westin Houston, Memorial City | 945 Gessner Road, Houston, TX 77024