Use Cases
Content routing by url
when HTTP_REQUEST {
local url = HTTP:url()
if url:find("^/sports") or url:find("^/news") or url:find("^/government") then
LB:routing("cr1")
debug("url %s starts with sports|news|government, routing to cr1\n", url)
elseif url:find("^/finance") or url:find("^/technology") or url:find("^/shopping") then
LB:routing("cr2")
debug("url %s starts with finance|technology|shopping, routing to cr2\n", url)
elseif url:find("^/game") or url:find("^/travel") then
LB:routing("cr3")
debug("url %s starts with game|travel, routing to cr3\n", url)
else
LB:routing("cr")
debug("No match for uri: %s, routing to default cr\n", url)
end
}
HTTP to HTTPS redirection for special ports
Only use the first port in HTTPS service.
when HTTP_REQUEST {
if not HTTP:is_https() then
local host = HTTP:header("host")[1]
local https_port = policy.https_ports()[1] -- get the first port in HTTP service
local newhost = host:gsub(":(%d+)", "") -- remove port from host if it has
if https_port ~= 443 then
-- if https port is not 443, add port to host
newhost = newhost .. ":" .. tostring(https_port)
end
HTTP:redirect("https://%s%s", newhost, HTTP:url())
end
HTTP connection will be closed if it finds any IP in IP reputation database during XFF headers full scanning
This function extracts all IPs from XFF headers and returns IP array.
function extract_xff(xff)
local t = {}
local k, v, s
for k, v in ipairs(xff) do
for s in v:gmatch("([^,]+)") do
t[#t + 1] = s:gsub("%s+", "")
end
end
return t
end
when HTTP_REQUEST {
local ips = extract_xff(HTTP:header("X-Forwarded-For"))
local r, i, v
for i, v in ipairs(ips) do
r = ip.reputation(v) -- check ip, will return an array
if #r > 0 then -- Found IP in reputation database
debug("Found bad IP %s in XFF headers, reputation: <%s>, GEO country: <%s>, GEO country code: %s\n",
v, table.concat(r, ', '),
ip.geo(v) or "unknown", ip.geo_code(v) or "unknown")
HTTP:close() -- force close this HTTP connection
return -- Stop script and return
end
end
}
When traffic originates only from the specified IP addresses, the URL should be redirected from /test1 to /test2. All other IPs should not be redirected.
-
1.1.1.1
-
2.2.2.2
when HTTP_REQUEST {
local ip_addresses = {"1.1.1.1", "2.2.2.2", "3.3.3.3"}
local skipIPs = {}
for _, ip in ipairs(ip_addresses) do
skipIPs[ip] = true
end
local url = HTTP:url()
local ip = tostring(IP:client_addr())
debug("url = %s, ip = %s, contains = %s ", url, ip, skipIPs[ip])
if skipIPs[ip] == nil and url == "/autotest/test1.html" then
debug("redirect")
HTTP:redirect("https://%s/autotest/upload/upload.html", HTTP:host())
end
}