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.

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.

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

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.

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
}