Requirement: Translate HTTP Host header value with pattern matching when forwarding client’s request to real server pool.
Client Request -> FortiADC VS: https://fadc-uat.lab.local
FortiADC VS -> real server pool: https://fadc-uat-int.lab.local
- Create Scripting under Server Load Balance -> Scripting -> Create New.
a. Define name. b. Input Lua script statement. c. Select Save.
when HTTP_REQUEST { -- Get the Host header hostname = HTTP:header_get_value("Host") debug("Original HTTP host is: %s\n", hostname) -- Perform pattern check using regex local match_result = string.match(hostname, "^fadc%-(.*)%.lab%.local$") if match_result then -- format of string.gsub(input, pattern, replacement) / "%1" in replacement is back-reference for call group in pattern new_host = string.gsub(hostname, "^fadc%-(.*)%.lab%.local$", "fadc-%1-int.lab.local") -- debug print to help troubleshoot issues with string/pattern matching debug("New HTTP host is: %s\n", new_host) -- Replace HTTP Host with new string replacement HTTP:header_replace("Host", new_host) LB:routing("DVWA_CR") else -- print as such if nothing is matched debug("HTTP Host did not match the pattern.\n") end }

Details and comments of the Lua script used in this article:

- Assign a virtual server with the created script under Server Load Balance -> Virtual Server, edit the respective virtual server, enable Scripting, and select the created script:

Demonstration: • Simulate request to FortiADC virtual server e.g https://fadc-uat.lab.local/login.php • Expect script to replace HTTP host when forwarding the request to the real server pool.

SLB traffic log: Client request to FortiADC virtual server

Packet capture: Client request forwarded to real server – HTTP host is replaced with a new value
Note:
In this demonstration, only the request HTTP Host is translated/replaced.
Debugging commands for troubleshooting:
diagnose debug module httproxy scripting set diagnose debug module httproxy scripting_minor set diagnose debug enable
Disabling debugging output:
diagnose debug disable diagnose debug module httproxy all unset
Related document:
FortiADC - HTTP Scripting
|