FortiADC
FortiADC enhances the scalability, performance, and security of your applications whether they are hosted on premises or in the cloud.
shafiq23
Staff
Staff
Article Id 299706
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.
    a. Define a name.
    b. Input 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\n", 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
}

 

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 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 response HTTP 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.

    script log.PNG

     

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

 

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