Fortinet white logo
Fortinet white logo

LB commands

LB commands

LB commands can be used in HTTP events.

LB:routing(“cr-name”)

Force current HTTP transaction to route to specific content routing.

Return value is Boolean. If the policy doesn’t have content routing or cannot find the specific content routing, return false. If routing successes, return true.

Syntax
LB:routing(cr-name)
Arguments

Name

Description

cr_name

String type content routing name.

Events

Applicable in HTTP_REQUEST.

Example
function startsWith(str, prefix)
    return string.sub(str, 1, #prefix) == prefix
end

when HTTP_REQUEST {
    local host = HTTP:host()
    if startsWith(host, "test1.com") then
        LB:routing("cr1")
    elseif startsWith(host, "test2.com") then
        LB:routing("cr2")
    end
}

LB:persist(“key”) / LB:persist(“key”, timeout)

Use the key string to do persistence. The type of the server pool’s persistence must be set to scripting, otherwise the function has no effect.

Please note the following:

  • If argument timeout doesn’t exist, use the default timeout in the persistence of the server pool.

  • If called in HTTP_REQUEST, the system will use the key to search the persistence table. If found, do persistence; If no found, insert key to the persistence table.

  • If called in HTTP_RESPONSE, the system will insert the key string to the persistence table.

Syntax
LB:persist(key) / LB:persist(key, timeout)
Arguments

Name

Description

key

String type persistence key

timeout

optional, integer type timeout, from 10-86400

Events

Applicable in HTTP_REQUEST, HTTP_RESPONSE, HTTP_DATA_REQUEST and HTTP_DATA_RESPONSE.

Examples

Do persistence in HTTP request header:

when HTTP_REQUEST {
    local jsession_id = HTTP:cookie("JSESSIONID")
    debug("jession_id=%s", jsession_id)
    if jsession_id then
        debug("jsession_id=%s", jsession_id)
        LB:persist(jsession_id)
    end
}

Do persistence in HTTP request body:

when HTTP_REQUEST {
    local uri = HTTP:url()
    debug("uri=%s", uri)
    if string.match(uri, "test_url") then
        HTTP:collect()
    end
}
when HTTP_DATA_REQUEST {
    local body_str = HTTP:body()
    local find_sessionID = body_str:find("persist=")

    if find_sessionID_2 then
        local start_pos = body_str:find("persist=")
        local sessionID = body_str:sub(start_pos + 8, start_pos + 8 + 3)
        debug("sessionID=%s", sessionID)
        LB:persist(sessionID)
    end
}

Do persistence in HTTP response header:

when HTTP_RESPONSE {
    local jsession_id = HTTP:cookie("JSESSIONID")
    local code, reason = HTTP:status()
    debug("code=%s", code)
    if jsession_id then
        -- if server respone has this cookie
        -- record the persistence to the persistence table
        debug("jsession_id=%s", jsession_id)
        LB:persist(jsession_id)
    end
}

Do persistence in HTTP response body:

when HTTP_RESPONSE {
    local code, reason = HTTP:status()
    debug("code=%s", code)
    if code == "302" then
        HTTP:collect()
    end
}
when HTTP_DATA_RESPONSE {
    local body_str = HTTP:body()
    local find_sessionID = body_str:find("persist=")
    if find_sessionID then
        local start_pos = body_str:find("persist=")
        local sessionID = body_str:sub(start_pos + 8, start_pos + 8 + 3)
        debug("sessionID=%s", sessionID)
        LB: persist(sessionID)
    end
}

LB:persist_remove(key)

Remove the persistence node based on the key

Syntax
LB:persist_remove(key)
Arguments

Name

Description

key

String type persistence key

Events

Applicable in HTTP_REQUEST, HTTP_RESPONSE, HTTP_DATA_REQUEST and HTTP_DATA_RESPONSE.

Example
when HTTP_REQUEST {
    local uri = HTTP:path()
    -- Extract the first folder from the URI using pattern matching
    -- For example: /api/v1/resource -> "api"
    local sysName = uri:match("^/([^/]+)/")
    local sysnamelower = sysName:lower()
    if sysnamelower then
        LB:persist(sysnamelower)
        debug("persist sys name: %s \n", sysnamelower)
    end
}
 
when HTTP_RESPONSE {
    local uri = HTTP:path()
    local status = HTTP:status()
    -- Extract the first folder from the URI using pattern matching
    -- For example: /api/v1/resource -> "api"
    local sysName = uri:match("^/([^/]+)/")
 
    if sysName then
        local sysnamelower = sysName:lower()
        if status == "503" then
            LB:persist_remove(sysnamelower)
            debug("status 503, remove %s persistence\n", sysnamelower)
        end
    end
}

LB commands

LB commands

LB commands can be used in HTTP events.

LB:routing(“cr-name”)

Force current HTTP transaction to route to specific content routing.

Return value is Boolean. If the policy doesn’t have content routing or cannot find the specific content routing, return false. If routing successes, return true.

Syntax
LB:routing(cr-name)
Arguments

Name

Description

cr_name

String type content routing name.

Events

Applicable in HTTP_REQUEST.

Example
function startsWith(str, prefix)
    return string.sub(str, 1, #prefix) == prefix
end

when HTTP_REQUEST {
    local host = HTTP:host()
    if startsWith(host, "test1.com") then
        LB:routing("cr1")
    elseif startsWith(host, "test2.com") then
        LB:routing("cr2")
    end
}

LB:persist(“key”) / LB:persist(“key”, timeout)

Use the key string to do persistence. The type of the server pool’s persistence must be set to scripting, otherwise the function has no effect.

Please note the following:

  • If argument timeout doesn’t exist, use the default timeout in the persistence of the server pool.

  • If called in HTTP_REQUEST, the system will use the key to search the persistence table. If found, do persistence; If no found, insert key to the persistence table.

  • If called in HTTP_RESPONSE, the system will insert the key string to the persistence table.

Syntax
LB:persist(key) / LB:persist(key, timeout)
Arguments

Name

Description

key

String type persistence key

timeout

optional, integer type timeout, from 10-86400

Events

Applicable in HTTP_REQUEST, HTTP_RESPONSE, HTTP_DATA_REQUEST and HTTP_DATA_RESPONSE.

Examples

Do persistence in HTTP request header:

when HTTP_REQUEST {
    local jsession_id = HTTP:cookie("JSESSIONID")
    debug("jession_id=%s", jsession_id)
    if jsession_id then
        debug("jsession_id=%s", jsession_id)
        LB:persist(jsession_id)
    end
}

Do persistence in HTTP request body:

when HTTP_REQUEST {
    local uri = HTTP:url()
    debug("uri=%s", uri)
    if string.match(uri, "test_url") then
        HTTP:collect()
    end
}
when HTTP_DATA_REQUEST {
    local body_str = HTTP:body()
    local find_sessionID = body_str:find("persist=")

    if find_sessionID_2 then
        local start_pos = body_str:find("persist=")
        local sessionID = body_str:sub(start_pos + 8, start_pos + 8 + 3)
        debug("sessionID=%s", sessionID)
        LB:persist(sessionID)
    end
}

Do persistence in HTTP response header:

when HTTP_RESPONSE {
    local jsession_id = HTTP:cookie("JSESSIONID")
    local code, reason = HTTP:status()
    debug("code=%s", code)
    if jsession_id then
        -- if server respone has this cookie
        -- record the persistence to the persistence table
        debug("jsession_id=%s", jsession_id)
        LB:persist(jsession_id)
    end
}

Do persistence in HTTP response body:

when HTTP_RESPONSE {
    local code, reason = HTTP:status()
    debug("code=%s", code)
    if code == "302" then
        HTTP:collect()
    end
}
when HTTP_DATA_RESPONSE {
    local body_str = HTTP:body()
    local find_sessionID = body_str:find("persist=")
    if find_sessionID then
        local start_pos = body_str:find("persist=")
        local sessionID = body_str:sub(start_pos + 8, start_pos + 8 + 3)
        debug("sessionID=%s", sessionID)
        LB: persist(sessionID)
    end
}

LB:persist_remove(key)

Remove the persistence node based on the key

Syntax
LB:persist_remove(key)
Arguments

Name

Description

key

String type persistence key

Events

Applicable in HTTP_REQUEST, HTTP_RESPONSE, HTTP_DATA_REQUEST and HTTP_DATA_RESPONSE.

Example
when HTTP_REQUEST {
    local uri = HTTP:path()
    -- Extract the first folder from the URI using pattern matching
    -- For example: /api/v1/resource -> "api"
    local sysName = uri:match("^/([^/]+)/")
    local sysnamelower = sysName:lower()
    if sysnamelower then
        LB:persist(sysnamelower)
        debug("persist sys name: %s \n", sysnamelower)
    end
}
 
when HTTP_RESPONSE {
    local uri = HTTP:path()
    local status = HTTP:status()
    -- Extract the first folder from the URI using pattern matching
    -- For example: /api/v1/resource -> "api"
    local sysName = uri:match("^/([^/]+)/")
 
    if sysName then
        local sysnamelower = sysName:lower()
        if status == "503" then
            LB:persist_remove(sysnamelower)
            debug("status 503, remove %s persistence\n", sysnamelower)
        end
    end
}